Java 将 ArrayList 传递给另一个类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20193243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:10:20  来源:igfitidea点击:

Pass ArrayList to another Class

javaclasscollectionsarraylist

提问by ZeroGS

I'm having some Trouble with an ArrayList. All i need to do is to pass a filled ArrayList to another Class so i can use the Values of the ArrayList there. Here's a snippet from the first Class:

我在使用 ArrayList 时遇到了一些麻烦。我需要做的就是将填充的 ArrayList 传递给另一个类,这样我就可以在那里使用 ArrayList 的值。这是第一堂课的片段:

public ArrayList<String> collCollect = new ArrayList<String>();
for (int i = 0; i < ListModel.size(); i++) {
        collCollect.add(ListModel.get(i).toString());
    }
    System.out.println(collCollect);

Till this Part everything is going quite well (i stripped the rest of the Code!)

直到这部分一切都进行得很顺利(我剥离了代码的其余部分!)

Now comes the tricky Part! This is the Second Class:

现在是棘手的部分!这是第二类:

ClassA pMain;
DecimalFormat f = new DecimalFormat("#0.00");
public ArrayList<String> listContent = new ArrayList<String>(pMain.collCollect);
DefaultListModel<String> ListModelNew = new DefaultListModel<String>();

public static void main(String args[]) {
for (int i = 0; i < listContent.size(); i++){
        ListModelNew.add(i, listContent.get(i));
    } 
}

Everytime the ClassB is loaded i get a NullPointerException from the Line where the reference to the Array in pMain is made.

每次加载 ClassB 时,我都会从引用 pMain 中的数组的行中得到 NullPointerException。

Any help would be appriciated...i'm unable to get the Values from ClassA ArrayList to ClassB -.-

任何帮助都会得到帮助...我无法从 ClassA ArrayList 到 ClassB 获取值 -.-

采纳答案by Kevin Bowersox

An instance of ClassAneeds to be created in the second class. I would recommend creating it within the main method so ClassAis not a dependency for the second class.

ClassA需要在第二个类中创建一个实例。我建议在 main 方法中创建它,因此ClassA它不是第二个类的依赖项。

DecimalFormat f = new DecimalFormat("#0.00");
public ArrayList<String> listContent; 
DefaultListModel<String> ListModelNew = new DefaultListModel<String>();

public static void main(String args[]) {
ClassA pMain = new ClassA();
listContent = new ArrayList<String>(pMain.collCollect);
for (int i = 0; i < listContent.size(); i++){
        ListModelNew.add(i, listContent.get(i));
    } 
}

This can be further refactored:

这可以进一步重构:

DecimalFormat f = new DecimalFormat("#0.00");
DefaultListModel<String> ListModelNew = new DefaultListModel<String>();

public static void main(String args[]) {
ClassA pMain = new ClassA();
for (int i = 0; i < pMain.collCollect.size(); i++){
        ListModelNew.add(i, pMain.collCollect.get(i));
    } 
}

回答by Prabhakaran Ramaswamy

ClassA pMain;// here you not initialized the object.

ClassA pMain;// 这里你没有初始化对象。

pMain.collCollect// here you are getting the NullpointerException

pMain.collCollect// 这里你得到了 NullpointerException

public ArrayList<String> listContent = new ArrayList<String>(pMain.collCollect);
                                                                    ^__see here   

change

改变

ClassA pMain;

to

ClassA pMain = new ClassA ();

回答by Michael Rose

Well you just declare the variable pMainbut never assign it anything. Thus it will be initialized with nulland give you a NullPointerExceptionwhen you try to access it.

好吧,您只是声明了变量,pMain但从不为其分配任何内容。因此,当您尝试访问它时,它将被初始化null并为您提供一个NullPointerException

You must either declare it like ClassA pMain = new ClassA()or assign a value elsewhere. If you assign the value elsewhere be sure to remove the direct initialization of listContent

您必须在ClassA pMain = new ClassA()其他地方声明它或分配一个值。如果你在别处赋值一定要去掉直接初始化listContent

回答by ioreskovic

Your ClassA pMainis not instantiated. You need to create the actual instance of ClassA like this:

ClassA pMain的没有被实例化。您需要像这样创建 ClassA 的实际实例:

ClassA pMain = new ClassA();

Since you're trying to get a public property of that class (collCollect), it would be sane to have that one exist too, either via constructor in ClassA or by calling some method on the newly created instance of ClassA.

由于您正在尝试获取该类的公共属性 (collCollect),因此也可以通过 ClassA 中的构造函数或通过在新创建的 ClassA 实例上调用某些方法来获得该类的公共属性。

回答by Matt

Instead of having the instantiator at the top of your class:

而不是将实例化器放在类的顶部:

public ArrayList<String> listContent = new ArrayList<String>(pMain.collCollect);

Override the constructor for your second class, and pass it in there:

覆盖第二个类的构造函数,并将其传递到那里:

public ClassB(ArrayList<String> pMain) {
  this.listContent = pMain.collCollect;
}

回答by Kishore

@Ben, You can keep array initialization code in collCollectInit() of ClassA. In second class create ClassA object as ClassA pMain=new ClassA(); pMain.collCollectInit(); then use pMain.collCollect if collCollect is a public or protected reference in ClassA. Please use getter and setter method to access class variable.

@Ben,您可以在 ClassA 的 collCollectInit() 中保留数组初始化代码。在第二个类中创建 ClassA 对象作为 ClassA pMain=new ClassA(); pMain.collCollectInit(); 如果 collCollect 是 ClassA 中的公共或受保护引用,则使用 pMain.collCollect。请使用 getter 和 setter 方法来访问类变量。

回答by ZeroGS

Ok i must have been blind not to initialize the ClassA in ClassB...that is fixed now. But the ListModelNew doesn't have any content it is out printed as "[]" so i think the ArrayList in ClassA is not passing the Values properly to ArrayList listContent in ClassB

好吧,我一定是瞎了,没有在 ClassB 中初始化 ClassA ......现在已经修复了。但是 ListModelNew 没有任何内容它被打印为“[]”所以我认为 ClassA 中的 ArrayList 没有将值正确传递给 ClassB 中的 ArrayList listContent