Java:int的可变大小数组

时间:2012-10-24 02:47:58

标签: java

嘿伙计们我正在尝试arraylists,我需要制作一个可变大小的整数数组。 我不知道我是否正确实施。 我该怎么做呢?有人可以用语法帮助我吗?

    Integer[] ints = new Integer[x];
    static List<Integer> ints = new ArrayList<Integer>();

我希望能够做像ints.add(0)这样的事情。 等

编辑:代码:

  Integer[] ints = new Integer[x]; 
  static List<Integer> ints = new ArrayList<Integer>(); 
  ints.clear(); 
  for (int counter2 = 0; counter2 < 50; counter2++) { 
      if (list[counter2].userActive == true) { 
        if (list[counter2].getLastName().equals(lastName) || (lastName=="")) { 
           if ((list[counter2].getFirstName().equals(firstName))||(firstName=="")
                && (list[counter2].userActive == true)) 
                ints.add(counter2);    

1 个答案:

答案 0 :(得分:1)

标准Java api没有可变大小的基元数组(除了引用类型)。有第三方库提供此功能(GNU trove被广泛使用;请参阅here以获取列表质量良好的库),或者您可以构建自己的库。

如果您不介意装箱/拆箱的开销,可以使用ArrayList<Integer>

List<Integer> list = new ArrayList<Integer>();

编辑查看您发布的代码,我看到的唯一错误是您将ints声明为Integer[]static List<Integer> 。您不能两次声明相同的变量。摆脱Integer[]声明,其余的代码看起来应该正常运行(至少关于将整数值添加到可变大小列表的部分;我对你的逻辑一无所知)。 / p>