My code runs an algorithm on an array, and stores the results in an ArrayList. The problem is that I am not able to access the contents of the ArrayList for subsequent processing. Though my actual code is thousands of lines long, I have trapped the problem, and have re-created the problem in the short code segments below. You can take the three classes below and run them in your IDE without changes to reproduce the problem yourself. As you can see, it populates the ArrayList within makeArrayList.java, but yet the contents of the ArrayList are not subsequently visible in getArrayList.java.
Can anyone show me how to fix the code below so that the contents of the ArrayList become visible/usable in getArrayList.java and in myGUI.java?
Here is the code for the three classes:
The code for myGUI.java is:
package arrayListPractice;
import java.awt.Dimension;
import javax.swing.JFrame;
public class myGUI extends JFrame {
public myGUI() {
super("test GUI");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(300, 200));
getArrayList getArrList = new getArrayList();
getArrList.getPeaks();
this.pack();}
public static void main(String args[]) {
myGUI myFrame = new myGUI();
myFrame.setVisible(true);}}
The code for getArrayList.java is:
package arrayListPractice;
import java.util.*;
public class getArrayList {
public static ArrayList<Integer> PeakList;
int myLength = 3500;
double[] myArray=new double[myLength];
public ArrayList<Integer> getPeaks(){
for(int h=0;h<myLength;h++){myArray[h]=Math.sqrt((double)h);}
PeakList = new makeArrayList(myArray,myLength);
System.out.println("in getArrayList.getPeaks, PeakList.size() is: "+PeakList.size());
return PeakList;}}
The code for makeArrayList.java is:
package arrayListPractice;
import java.util.*;
public class makeArrayList extends ArrayList<Integer> {
ArrayList<Integer> myArrayList= new ArrayList<Integer>();
public makeArrayList(double[] myArray, int arrayLength) {
// NOTE: My actual code does many transformations to myArray. The resulting myArrayList
// contains only 1/1000 of the points in myArray. This code is just simplified for debugging.
for(int i=0;i<arrayLength;i++){myArrayList.add((int)Math.pow(myArray[i],2));}
System.out.println("in makeArrayList, PeakList.size() is: "+myArrayList.size());}}
You are confusing and combining inheritance and composition in the same class:
class makeArrayList extends ArrayList<Integer> {
ArrayList<Integer> myArrayList = new ArrayList<Integer>();
public makeArrayList(double[] myArray, int arrayLength) {
// NOTE: My actual code does many transformations to myArray. The
// resulting myArrayList
// contains only 1/1000 of the points in myArray. This code is just
// simplified for debugging.
for (int i = 0; i < arrayLength; i++) {
myArrayList.add((int) Math.pow(myArray[i], 2));
}
System.out.println("in makeArrayList, PeakList.size() is: "
+ myArrayList.size());
}
}
Note that this class both contains an ArrayList and extends ArrayList and you're trying to make both ArrayLists interchangeable , but they're not.
Some Suggestions:
e.g.,
import java.util.ArrayList;
public class MyNonGUI2 {
public static void main(String args[]) {
GetArrayList2 getArrList = new GetArrayList2();
getArrList.getPeaks();
}
}
class GetArrayList2 {
public ArrayList<Integer> PeakList;
int myLength = 3500;
double[] myArray = new double[myLength];
public ArrayList<Integer> getPeaks() {
for (int h = 0; h < myLength; h++) {
myArray[h] = Math.sqrt((double) h);
}
PeakList = new MakeArrayList2(myArray, myLength).getArrayList();
System.out.println("in GetArrayList2.getPeaks, PeakList.size() is: "
+ PeakList.size());
return PeakList;
}
}
class MakeArrayList2 {
ArrayList<Integer> myArrayList = new ArrayList<Integer>();
public MakeArrayList2(double[] myArray, int arrayLength) {
for (int i = 0; i < arrayLength; i++) {
myArrayList.add((int) Math.pow(myArray[i], 2));
}
System.out.println("in MakeArrayList2, PeakList.size() is: "
+ myArrayList.size());
}
public int size() {
return myArrayList.size();
}
public ArrayList<Integer> getArrayList() {
return new ArrayList<Integer>(myArrayList);
}
}