2

I'm having trouble retreiving data from a list. The values never seem to get stored, or the object is initialized? I made a class that stores some variables:

public class storage{
     public int a = 0;
     public int b = 0;
}

then I created another class which fills a, and b with some values and stores the object in a list

public class anotherclass{
      public List<storage> alldata = new ArrayList<storage>();

      public void filldata(){
            storage tmp = new storage();
            for (int i = 1; i <= 10; i++){
                 tmp.a = i;
                 tmp.b = i;
                 alldata.add(tmp);
            }
      }
}

but when I run filldata() in my main class, then try to get the object from the list a and b are still set at 0.

public static void main(String[] args){
    anotherclass obj = new anotherclass();
    obj.filldata()

    for (int i = 0; i <= obj.alldata.size() - 1; i++){
          System.out.println(obj.alldata.get(i).a)
          System.out.println(obj.alldata.get(i).b)
          //Outputs as all zeroes
    }

}

How could this be?

4

5 回答 5

7

尝试每次创建对象。否则它使用相同的对象。所以只有它不会添加到您的列表中。

            storage tmp = null;
            for (int i = 1; i <= 10; i++)
            {
                 tmp = new storage();
                 tmp.a = i;
                 tmp.b = i;
                 alldata.add(tmp);
            }

代替

            storage tmp = new storage();
            for (int i = 1; i <= 10; i++)
            {
                 tmp.a = i;
                 tmp.b = i;
                 alldata.add(tmp);
            }
于 2013-10-11T03:56:17.177 回答
2

下面的行应该在方法的for循环内filldata()

storage tmp = new storage();

因为每次您需要创建一个新对象以添加到您的列表中。在您的情况下,同一个对象将被新值覆盖。

此外,您的main方法的语法是错误的。它应该是

public static void main(String[] args)

旁注:复制粘贴来自 IDE 的错误代码。请不要在这里输入。您的代码存在很多编译问题(语法错误、缺少分号等)。我之所以这么说是因为我提到的解决方案是针对您将所有 *10*s 作为输出而不是 *0*s 的情况。如果您得到 *0*s,那么您发布的代码不是给出该问题的代码。

于 2013-10-11T03:57:08.810 回答
1

您的 filldata() 方法存在指针缺陷。您只创建了一个对象 tmp 并将同一个对象添加到整个列表中。列表中的所有条目都指向同一个对象。要解决您的问题,您应该首先为每个条目创建一个新对象。

将您的 filldata() 替换为:

public void filldata(){
        storage tmp;
        for (int i = 1; i <= 10; i++){
             tmp = new storage();
             tmp.a = i;
             tmp.b = i;
             alldata.add(tmp);
        }
  }
于 2013-10-11T04:01:43.207 回答
0

这是您的程序,它打印所有 10(而不是您所说的 0),这是错误的。根据@newuser 的回答进行修改

import java.util.*;

class storage{
     public int a = 0;
     public int b = 0;
}

public class anotherclass{
      public List<storage> alldata = new ArrayList<storage>();

      public void filldata(){
            storage tmp = new storage();
            for (int i = 1; i <= 10; i++){
                 tmp.a = i;
                 tmp.b = i;
                 alldata.add(tmp);
            }
      }

public static void main(String[] args){
    anotherclass obj = new anotherclass();
    obj.filldata();

    for (int i = 0; i <= obj.alldata.size() - 1; i++){
          System.out.println(obj.alldata.get(i).a);
          System.out.println(obj.alldata.get(i).b);

    }

}

}
于 2013-10-11T03:59:40.230 回答
0

希望这对您有所帮助,即使上面的代码也有内部类静态引用错误。下面new storage();的示例使用for循环创建一个新对象并在每次迭代时获取新的 a 和 b值。

import java.util.ArrayList;
import java.util.List;

public class StackClass {

class Storage {

    public int a = 0;
    public int b = 0;
}

public class AnotherClass {

    public List<Storage> allData = new ArrayList<Storage>();

    public void fillData() {

        for (int i = 1; i <= 10; i++) {
            Storage tmp = new Storage();
            tmp.a = i;
            tmp.b = i;
            allData.add(tmp);
        }
    }
}

public static void main(String[] args) {

    StackClass stack = new StackClass();
    AnotherClass obj = stack.new AnotherClass();        

    obj.fillData();

    for (int i = 0; i <= obj.allData.size() - 1; i++) {
        System.out.println("Objec[" + i + "].a-" + obj.allData.get(i).a);
        System.out.println("Objec[" + i + "].b-" + obj.allData.get(i).b);

    }
}
}
于 2013-10-11T04:36:28.950 回答