4

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());}}
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • 2
    +1 for the effort in creating a [SSCCE: Short Self-Contained Correct Example](http://pscode.org/sscce.html) – Bohemian Sep 09 '11 at 02:13
  • Thank you. It took a long time just to trap the error and then to reproduce it. – CodeMed Sep 09 '11 at 02:56
  • I was able to use the solution to fix the problem in my code. Therefore, I am marking this question as answered now. – CodeMed Sep 10 '11 at 00:01

1 Answers1

4

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:

  • There's no need for this class to extend ArrayList, so get rid of the extends and instead simplify and clarify things by just using composition.
  • Avoid using static anything unless you have a very good reason for doing so. This is not part of your main problem, but is a problem with your sample code.
  • For the sake of others reading your code and either helping you or grading you, don't be afraid to use whitespace to make your code more readable. Page real estate is not that expensive. Also read up on and use Java naming conventions including capitalizing the first letter of class names. This will make it much easier for others (us!) to read and understand your code.

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);
   }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thank you very much. I deleted the extension of ArrayList, but it now gives the following error: "Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from makeArrayList to ArrayList at arrayListPractice.getArrayList.getPeaks(getArrayList.java:11)". – CodeMed Sep 09 '11 at 02:12
  • Yeah, since it's not extending an array list you have to alter your code, of course. Think it through and you'll figure it out. – Hovercraft Full Of Eels Sep 09 '11 at 02:15
  • Thank you. I tried your suggestion, and eclipse is giving the error: "Type mismatch: cannot convert from makeArrayList to ArrayList" for the line: "PeakList = new MakeArrayList2(myArray, myLength).getArrayList();" in your GetArrayList2. – CodeMed Sep 09 '11 at 02:49
  • I do read and study continually. And this problem is inspiring me to start another broad exploration of the topics you mentioned. But I have been at this problem for a couple of days now, and I was hoping to find an explicit answer to this problem from this forum, if possible. The underlying algorithm took a month to write, and debugging it will require a lot more work on my part. I don't want to wait a week to start debugging the underlying algorithm if someone is kind enough to spend a few minutes saving me a few more days of this step. Thank you again. – CodeMed Sep 09 '11 at 02:52
  • 2
    ??? What don't you understand about my comments and code? I have written a full code solution at least for your posted code over 40 minutes ago, I'm not sure what more you expect or need? – Hovercraft Full Of Eels Sep 09 '11 at 02:58
  • The error came the first time I tried to integrate your solution into my pre-existing code. Now that I have just copied your code verbatim, it seems to run correctly. However, I want to understand how and why this works, and to also integrate a working solution into my actual, much more complex, code. I will have to do that tomorrow because it is late here now. I will reply to this again tomorrow, perhaps 18 hours from now, if not sooner. – CodeMed Sep 09 '11 at 05:14
  • I was able to use your suggestions to fix the problem in my application. Thank you. – CodeMed Sep 10 '11 at 00:00
  • @CodeMed: Great! Glad the suggestions helped! – Hovercraft Full Of Eels Sep 10 '11 at 00:08