Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6541071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:03:33+00:00 2026-05-25T11:03:33+00:00

My code runs an algorithm on an array, and stores the results in an

  • 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());}}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:03:34+00:00Added an answer on May 25, 2026 at 11:03 am

    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);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a few modules of code in Access vba. Each code runs
I have a simple jQuery code that runs on a web page that has
Can an NSArray hold an array of bool values? The following code runs BOOL
i have an application project that both managed and unmanaged code runs and i
I have a piece of code that looks like this: Algorithm a = null;
My code runs inside a JAR file, say foo.jar , and I need to
The next code runs normally in SQLSERVER but when i change the web.config to
I'm trying to programmatically add dots to a radar screen. The code runs without
i've got the following code which runs a bat file. the bat file then
I have the following jQuery code which runs when I'm clicking an option in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.