开发者

ArrayList composed of various types of objects

I have a class that needs to return multiple data objects of various types, such as an ArrayList<Integer> and arrays of double[]. Since java only allows one object to be returned by a given method, I am trying to bundle the various data objects into an ArrayList. However, there are two problems:

  1. The code I am coming up with is unable to read the type of object in each index of the ArrayList.

    Specifically, in ListOfObjects.java below, Eclipse gives me an error message stating Type mismatch: cannot convert from Object to ArrayList<Integer> at the line myAL1=dataHolder.get(0);, followed by similar error messages for the three other get statements that follow it.

  2. I do not know what type to specify as the data type for the ArrayList.

My code is in two files below.

Can anyone show me how to fix it so that these two problems are fixed?

I need to be able to subsequently use myAL1 as an ArrayList, and to use myDBL1, mtDBL2, and myDBL3 as double[].

ListOfObjects.java

import java.util.ArrayList;

public class ListOfObjects {
    ArrayList<Integer> myAL1 = new ArrayList<Integer>();
    double[] myDBL1 = new double[25];
    double[] myDBL2 = new double[25];
    double[] myDBL3 = new double[25];

    public static void main(String[] args) {
    }

    public void myMethod() {
        AssembleListOfObjects myLOO = new AssembleListOfObjects();
        ArrayList dataHolder = myLOO.buildListOfObjects();
        myAL1 = dataHolder.get(0);
        myDBL1 = dataHolder.get(0);
        myDBL2 = dataHolder.get(0);
        myDBL3 = dataHolder.get(0);
    }
}

AssembleListOfObjects.java

import java.util.ArrayList;

public class AssembleListOfObjects {
    ArrayList<Integer> al1 = new ArrayList<Integer>();
    double[] dbl1 = new double[25];
    double[] dbl2 = new double[25];
    double[] dbl3 = new double[25];

    public void main(String[] args) {
        buildListOfObjects();
    }

    public ArrayList开发者_高级运维 buildListOfObjects() {
        ArrayList ListOfObjects = new ArrayList();
        ListOfObjects.add(al1);
        ListOfObjects.add(dbl1);
        ListOfObjects.add(dbl2);
        ListOfObjects.add(dbl3);
        return ListOfObjects;
    }
}

EDIT:

I re-wrote it your way, and here is what I have so far. It throws a java.lang.NoSuchMethodError: main error unless I add the static modifier everywhere in the code.

When I do add the static modifier everywhere, it prints out arrays of zeros and an empty arraylist.

How would you fix this code so that ListOfObjects is able to output values for each of the arrays/arraylist?

But here is the code:

ListOfObjects.java

import java.util.ArrayList;

public class ListOfObjects {
    ArrayList<Integer> myAL1 = new ArrayList<Integer>();
    double[] myDBL1 = new double[25];
    double[] myDBL2 = new double[25];
    double[] myDBL3 = new double[25];

    public void main(String[] args) {
        myMethod();
    }

    public void myMethod() {
        AssembleListOfObjects myLOO = new AssembleListOfObjects();
        myAL1 = myLOO.getAl1();
        myDBL1 = myLOO.getDbl1();
        myDBL2 = myLOO.getDbl2();
        myDBL3 = myLOO.getDbl3();

        System.out.print("myAL1 is: (");
        for (int l = 0; l < myAL1.size(); l++) {
            if (l == 0) {
                System.out.print(myAL1.get(l));
            } else {
                System.out.print(", " + myAL1.get(l));
            }
        }
        System.out.println(")");

        System.out.print("myDBL1 is: (");
        for (int l = 0; l < myDBL1.length; l++) {
            if (l == 0) {
                System.out.print(myDBL1[l]);
            } else {
                System.out.print(", " + myDBL1[l]);
            }
        }
        System.out.println(")");

        System.out.print("myDBL2 is: (");
        for (int l = 0; l < myDBL2.length; l++) {
            if (l == 0) {
                System.out.print(myDBL2[l]);
            } else {
                System.out.print(", " + myDBL2[l]);
            }
        }
        System.out.println(")");

        System.out.print("myDBL3 is: (");
        for (int l = 0; l < myDBL3.length; l++) {
            if (l == 0) {
                System.out.print(myDBL3[l]);
            } else {
                System.out.print(", " + myDBL3[l]);
            }
        }
        System.out.println(")");
    }
}

AssembleListOfObjects.java

import java.util.ArrayList;

public class AssembleListOfObjects {
    private ArrayList<Integer> al1 = new ArrayList<Integer>();
    int mySize = 25;
    private double[] dbl1 = new double[mySize];
    private double[] dbl2 = new double[mySize];
    private double[] dbl3 = new double[mySize];

    public void main(String[] args) {
        setterMethod();
        getAl1();
        getDbl1();
        getDbl2();
        getDbl3();
    }

    public void setterMethod() {
        for (int j = 0; j < mySize; j++) {
            // the following lines are placeholders for a complex algorithm
            dbl1[j] = j;
            dbl2[j] = Math.pow((double) j, 3);
            dbl3[j] = Math.cos((double) j);
            if ((j % 3) == 0) {
                al1.add(j);
            }
        }
    }

    public ArrayList<Integer> getAl1() {
        return al1;
    }

    public double[] getDbl1() {
        return dbl1;
    }

    public double[] getDbl2() {
        return dbl2;
    }

    public double[] getDbl3() {
        return dbl3;
    }
}


It's a very bad design decision to try to return mixed types in an array list and suggests that your design is off. If you're always manipulating the ArrayList of Integer and 3 double arrays, why not put them in a class, here you call AssembleListOfObjects, and give that class public getter or accessor methods to get the ArrayList and to get the 3 double arrays individually? Then if you need a method to manipulate this information and return it, it can simply return an object of this class, and whoever calls the method can extract the information it needs by calling the appropriate getter method.

e.g.,

import java.util.ArrayList;

public class AssembleListOfObjects {
   private ArrayList<Integer> al1 = new ArrayList<Integer>();

   // Also this can be a 2-dimensional array of double
   private double[] dbl1 = new double[25];
   private double[] dbl2 = new double[25];
   private double[] dbl3 = new double[25];

   public ArrayList<Integer> getAl1() {
      return al1;
   }
   public double[] getDbl1() {
      return dbl1;
   }
   public double[] getDbl2() {
      return dbl2;
   }
   public double[] getDbl3() {
      return dbl3;
   }

   // public void setter methods
   // and any other data manipulation methods

}

Looking at your newly edited code, I modified it some including adding a static modifier to your main method so it is a try main method, and calling myMethod inside on a new ListOfObjects object since myMethod cannot be called in a static context but must be called off of an appropriate object. I've also a call to myLOO.setterMethod(); from within myMethod:

import java.util.ArrayList;

public class ListOfObjects {
   ArrayList<Integer> myAL1 = new ArrayList<Integer>();
   double[] myDBL1 = new double[25];
   double[] myDBL2 = new double[25];
   double[] myDBL3 = new double[25];

   // added static to main method
   public static void main(String[] args) {
      // commented out as this can't be called in a static context, but 
      // needs to be called on an object
      // myMethod(); 


      // created a ListOfObjects object and called myMethod on it
      ListOfObjects myListOfObjs = new ListOfObjects();
      myListOfObjs.myMethod();
   }

   public void myMethod() {
      AssembleListOfObjects myLOO = new AssembleListOfObjects();
      myLOO.setterMethod();  // *** added
      myAL1 = myLOO.getAl1();
      myDBL1 = myLOO.getDbl1();
      myDBL2 = myLOO.getDbl2();
      myDBL3 = myLOO.getDbl3();

      System.out.print("myAL1 is: (");
      for (int l = 0; l < myAL1.size(); l++) {
         if (l == 0) {
            System.out.print(myAL1.get(l));
         } else {
            System.out.print(", " + myAL1.get(l));
         }
      }

      System.out.println(")");

      System.out.print("myDBL1 is: (");
      for (int l = 0; l < myDBL1.length; l++) {
         if (l == 0) {
            System.out.print(myDBL1[l]);
         } else {
            System.out.print(", " + myDBL1[l]);
         }
      }

      System.out.println(")");

      System.out.print("myDBL2 is: (");
      for (int l = 0; l < myDBL2.length; l++) {
         if (l == 0) {
            System.out.print(myDBL2[l]);
         } else {
            System.out.print(", " + myDBL2[l]);
         }
      }

      System.out.println(")");

      System.out.print("myDBL3 is: (");
      for (int l = 0; l < myDBL3.length; l++) {
         if (l == 0) {
            System.out.print(myDBL3[l]);
         } else {
            System.out.print(", " + myDBL3[l]);
         }
      }
      ;
      System.out.println(")");

   }
}

class AssembleListOfObjects {
   private ArrayList<Integer> al1 = new ArrayList<Integer>();
   int mySize = 25;
   private double[] dbl1 = new double[mySize];
   private double[] dbl2 = new double[mySize];
   private double[] dbl3 = new double[mySize];

   public void main(String[] args) {
      setterMethod();
      getAl1();
      getDbl1();
      getDbl2();
      getDbl3();
   }

   public void setterMethod() {
      for (int j = 0; j < mySize; j++) {
         // the following lines are placeholders for a complex algorithm
         dbl1[j] = j;
         dbl2[j] = Math.pow((double) j, 3);
         dbl3[j] = Math.cos((double) j);
         if ((j % 3) == 0) {
            al1.add(j);
         }
      }
   }

   public ArrayList<Integer> getAl1() {
      return al1;
   }

   public double[] getDbl1() {
      return dbl1;
   }

   public double[] getDbl2() {
      return dbl2;
   }

   public double[] getDbl3() {
      return dbl3;
   }
}


The way to get around the compiler errors is to use explicit casts, e.g. like this:

ArrayList dataHolder=myLOO.buildListOfObjects();
myAL1=(ArrayList<Integer>)dataHolder.get(0);
myDBL1=(double[])dataHolder.get(0);

However, as already mentioned, this is bad design and you should probably bundle it in a class or something like this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜