Java ArrayList 内容不传播
我的代码在数组上运行算法,并将结果存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在同一个类中混淆并组合了继承和组合:
请注意,该类都包含 ArrayList 和 扩展 ArrayList 并且您试图使两个 ArrayList 可以互换,但它们不能互换。
一些建议:
例如,
You are confusing and combining inheritance and composition in the same class:
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:
e.g.,