Java ArrayList Content Not Distributed

My code runs the algorithm in an array and stores the results in an ArrayList. The problem is that I cannot access the contents of the ArrayList for later processing. Although my actual code lasts thousands of lines, I got into a problem and created the problem again 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 fills the ArrayList in makeArrayList.java, but still the contents of the ArrayList array are not subsequently displayed in getArrayList.java.

Can someone show me how to fix the code below so that the contents of the ArrayList become visible / useful in getArrayList.java and in myGUI.java?

Here is the code for the three classes:

Code myGUI.java:

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);}} 

Code for getArrayList.java:

 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;}} 

Code makeArrayList.java:

 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());}} 
+4
source share
1 answer

You mix and combine inheritance and composition in one 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 contains an ArrayList and extends ArrayList, and you are trying to make both ArrayLists interchangeable, but this is not the case.

Some suggestions:

  • There is no need for this class to extend ArrayList, so get rid of extensions and instead simplify and refine tags just by using composition.
  • Avoid using static things unless you have a good reason to do so. This is not part of your main problem, but a problem with your sample code.
  • To have others read your code and help you or rate you, don't be afraid to use spaces to make your code more readable. Property page is not so expensive. Also read and use the Java naming conventions, including the capital letters of the first letter of class names. This will make it easier for others (us!) To read and understand your code.

eg.

 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); } } 
+4
source

All Articles