Quantcast
Channel: Active questions tagged propagation - Stack Overflow
Viewing all articles
Browse latest Browse all 52

Java ArrayList contents not propagating

$
0
0

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

Viewing all articles
Browse latest Browse all 52

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>