Note that there are some explanatory texts on larger screens.

plurals
  1. POArrayList composed of various types of objects
    primarykey
    data
    text
    <p>I have a class that needs to return multiple data objects of various types, such as an <code>ArrayList&lt;Integer&gt;</code> and arrays of <code>double[]</code>. Since java only allows one object to be returned by a given method, I am trying to bundle the various data objects into an <em>ArrayList</em>. However, there are two problems:</p> <ol> <li><p>The code I am coming up with is unable to read the type of object in each index of the ArrayList.<br> Specifically, in <em>ListOfObjects.java</em> below, Eclipse gives me an error message stating <code>Type mismatch: cannot convert from Object to ArrayList&lt;Integer&gt;</code> at the line <code>myAL1=dataHolder.get(0);</code>, followed by similar error messages for the three other get statements that follow it.</p></li> <li><p>I do not know what type to specify as the data type for the <em>ArrayList</em>.</p></li> </ol> <p>My code is in two files below.<br> Can anyone show me how to fix it so that these two problems are fixed?</p> <p>I need to be able to subsequently use <strong>myAL1</strong> as an <em>ArrayList</em>, and to use <strong>myDBL1</strong>, <strong>mtDBL2</strong>, and <strong>myDBL3</strong> as <em>double[]</em>.</p> <p><strong>ListOfObjects.java</strong></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]; 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); } } </code></pre> <p><strong>AssembleListOfObjects.java</strong></p> <pre><code>import java.util.ArrayList; public class AssembleListOfObjects { ArrayList&lt;Integer&gt; al1 = new ArrayList&lt;Integer&gt;(); 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; } } </code></pre> <h2>EDIT:</h2> <p>I re-wrote it your way, and here is what I have so far. It throws a <code>java.lang.NoSuchMethodError: main</code> error unless I add the <strong>static</strong> modifier everywhere in the code.<br> When I do add the static modifier everywhere, it prints out arrays of zeros and an empty arraylist.<br> How would you fix this code so that <em>ListOfObjects</em> is able to output values for each of the arrays/arraylist?</p> <p>But here is the code:</p> <p><strong>ListOfObjects.java</strong></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]; 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 &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(")"); } } </code></pre> <p><strong>AssembleListOfObjects.java</strong></p> <pre><code>import java.util.ArrayList; public 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.
    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.
 

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