Java ArrayList 内容不传播

发布于 2024-12-03 16:36:59 字数 2051 浏览 3 评论 0原文

我的代码在数组上运行算法,并将结果存储在 ArrayList 中。问题是我无法访问 ArrayList 的内容以进行后续处理。尽管我的实际代码有数千行长,但我已经捕获了问题,并在下面的短代码段中重新创建了问题。您可以使用下面的三个类并在 IDE 中运行它们而不进行任何更改,以自行重现问题。正如您所看到的,它填充了 makeArrayList.java 中的 ArrayList,但 ArrayList 的内容随后在 getArrayList.java 中不可见。

谁能告诉我如何修复下面的代码,以便 ArrayList 的内容在 getArrayList.java 和 myGUI.java 中变得可见/可用?

以下是这三个类的代码:

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熟人话多 2024-12-10 16:36:59

您在同一个类中混淆并组合了继承和组合:

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

请注意,该类都包含 ArrayList 扩展 ArrayList 并且您试图使两个 ArrayList 可以互换,但它们不能互换。

一些建议:

  • 此类不需要扩展 ArrayList,因此摆脱扩展,而仅通过使用组合来简化和澄清事物。
  • 避免使用静态任何东西,除非你有充分的理由这样做。这不是您的主要问题的一部分,而是您的示例代码的问题。
  • 为了其他人阅读您的代码并帮助您或为您评分,不要害怕使用空格来使您的代码更具可读性。页面房地产并不那么昂贵。另请阅读并使用 Java 命名约定,包括将类名的首字母大写。这将使其他人(我们!)更容易阅读和理解您的代码。

例如,

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

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);
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文