Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>e.g.,</p> <pre><code>import java.util.ArrayList; public class AssembleListOfObjects { private ArrayList&lt;Integer&gt; al1 = new ArrayList&lt;Integer&gt;(); // 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&lt;Integer&gt; 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 } </code></pre> <p>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 <code>myLOO.setterMethod();</code> from within myMethod:</p> <pre><code>import java.util.ArrayList; public class ListOfObjects { ArrayList&lt;Integer&gt; myAL1 = new ArrayList&lt;Integer&gt;(); 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 &lt; 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 &lt; 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 &lt; 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 &lt; myDBL3.length; l++) { if (l == 0) { System.out.print(myDBL3[l]); } else { System.out.print(", " + myDBL3[l]); } } ; System.out.println(")"); } } class AssembleListOfObjects { private ArrayList&lt;Integer&gt; al1 = new ArrayList&lt;Integer&gt;(); 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 &lt; 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&lt;Integer&gt; getAl1() { return al1; } public double[] getDbl1() { return dbl1; } public double[] getDbl2() { return dbl2; } public double[] getDbl3() { return dbl3; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload