简体   繁体   English

将ArrayList传递给另一个类

[英]Pass ArrayList to another Class

I'm having some Trouble with an ArrayList. 我对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. 我需要做的就是将一个填充的ArrayList传递给另一个Class,这样我就可以使用ArrayList的值了。 Here's a snippet from the first Class: 这是第一堂课的片段:

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时,我都会从Line获得一个NullPointerException,在该行中对pMain中的Array进行引用。

Any help would be appriciated...i'm unable to get the Values from ClassA ArrayList to ClassB -.- 任何帮助都会被appriciated ...我无法从ClassA ArrayList获取值到ClassB -.-

ClassA pMain; // here you not initialized the object. //这里你没有初始化对象。

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

An instance of ClassA needs to be created in the second class. 需要在第二个类中创建ClassA的实例。 I would recommend creating it within the main method so ClassA is not a dependency for the second class. 我建议在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));
    } 
}

Your ClassA pMain is not instantiated. 您的ClassA pMain未实例化。 You need to create the actual instance of ClassA like this: 您需要像这样创建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实例上调用某个方法来存在该属性也是理智的。

Well you just declare the variable pMain but never assign it anything. 那么你只需声明变量pMain但从不分配任何东西。 Thus it will be initialized with null and give you a NullPointerException when you try to access it. 因此,它将使用null初始化,并在您尝试访问它时给出NullPointerException

You must either declare it like ClassA pMain = new ClassA() or assign a value elsewhere. 您必须将其声明为ClassA pMain = new ClassA()或在其他位置指定值。 If you assign the value elsewhere be sure to remove the direct initialization of listContent 如果在其他地方分配值,请务必删除listContent的直接初始化

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

@Ben, You can keep array initialization code in collCollectInit() of ClassA. @Ben,您可以在ClassA的collCollectInit()中保留数组初始化代码。 In second class create ClassA object as ClassA pMain=new ClassA(); 在第二个类中创建ClassA对象作为ClassA pMain = new ClassA(); pMain.collCollectInit(); pMain.collCollectInit(); then use pMain.collCollect if collCollect is a public or protected reference in ClassA. 然后使用pMain.collCollect如果collCollect是ClassA中的公共或受保护引用。 Please use getter and setter method to access class variable. 请使用getter和setter方法访问类变量。

Ok i must have been blind not to initialize the ClassA in ClassB...that is fixed now. 好吧,我一定是盲目的,不能在ClassB中初始化ClassA ......现在已经修复了。 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 但ListModelNew没有任何打印为“[]”的内容,所以我认为ClassA中的ArrayList没有正确地将值传递给ClassB中的ArrayList listContent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM