Java arraylist始终以恒定容量创建

Java arraylist始终以恒定容量创建,java,arraylist,Java,Arraylist,我试图计算给定行号的pascal三角形。我正在使用递归 我的代码如下: public static List<Integer> getRow(int rowIndex) { if (rowIndex == 1){ List <Integer> list = new ArrayList(rowIndex+1); list.add(1); return list; } els

我试图计算给定行号的pascal三角形。我正在使用递归

我的代码如下:

public static List<Integer> getRow(int rowIndex) {

      if (rowIndex == 1){
          List <Integer> list = new ArrayList(rowIndex+1);
          list.add(1);
          return list;
      }
        else{
              List<Integer> oldList = getRow(rowIndex -1);
              List <Integer> list = new ArrayList(rowIndex+1);
              int temp = 0;
              list.add(0,1);
              list.add(list.size()-1,1);
              System.out.println("rowIndex "+rowIndex);

              for (int i = 1; i < list.size()-1; i ++){
                 temp = oldList.get(i) + oldList.get(i-1);
                 list.add(i,temp);
              }
              return list;

        }
    }
不管我想得到哪一行,它总是返回[1,1]。我试着插入打印语句。我注意到不管rowIndex是什么,列表的大小总是2

 List <Integer> list = new ArrayList(rowIndex+1);

上面的行不是创建ArrayList的正确方法吗?似乎我的arraylist的大小始终为2

我想你没法解释数据结构

数组列表是在数组顶部实现的列表。在构造函数中设置数组的大小是让开发人员控制数组初始大小的一种方法。这很少是必要的,因为类本身管理数组大小,所以只需忽略此参数。因此数组列表的大小实际上是列表的大小,它是元素的数量,而不是构造函数中指定的底层数组中的bucket数量

如果您知道所需数组的大小,并且希望在特定位置获取和添加,请使用标准数组,而不是数组列表

但是,我认为如果您移动,您的代码将起作用

list.add(list.size()-1,1);
在您的for循环之后,我实际上很惊讶它没有抛出索引越界异常。而且,由于是从左到右,所有的add都不需要指定索引,因为它只会将其添加到现有列表的末尾。

您误解了ArrayList的工作方式,您确实应该这样做

简而言之,构造函数的参数定义内存中ArrayList的初始大小,而不是最大大小。如果实例化一个新的ArrayList 2,这只意味着jvm预先为两个整数分配足够的空间,并且当您添加第三个元素时,jvm将增大ArrayList的大小,以便允许您添加更多的元素

此外,仅当在该位置添加了元素时,才可以使用get访问ArrayList位置

最后,请记住,在特定位置添加会向右移动所有元素。因此,如果您添加10,1,然后添加2,4,您的第一个添加将右移

回到您的问题,如果您绝对希望使用ArrayList而不是数组,则必须使用正确的大小初始化ArrayList,然后在正确的位置设置值

以下是一个可行的解决方案:

// the method with your algorithm which has been slightly modified  
public static List<Integer> getRow(final int rowIndex) {
    // notice that I call a helper method which initialises correctly the ArrayList
    final List<Integer> list = init(rowIndex);
    if (rowIndex == 1) {
        // notice that I set the value at a given position
        // I can only do it because I initialised all values to 0 first
        list.set(0, 1);
    } else {
        final List<Integer> previousRowList = getRow(rowIndex - 1);
        // again, I set values...
        list.set(0, 1);
        list.set(rowIndex - 1, 1);
        for (int i = 1; i < (list.size() - 1); i++) {
            // set again...
            list.set(i, previousRowList.get(i - 1) + previousRowList.get(i));
        }
    }
    // lets print out the row
    System.err.println(list);
    // then return it
    return list;
}

public static List<Integer> init(final int size) {
    // passing the size is overkill, but well...
    final List<Integer> list = new ArrayList<Integer>(size);
    // fill the ArrayList with zeros
    for (int i = 0; i < size; i++) {
        list.add(i, 0);
    }
    // then return it
    return list;
}

public static void main(final String[] args) {
    getRow(Integer.parseInt(args[0]));
}

希望有帮助

ArrayList容量是无关的,除了假定的性能调优目的;然后是list.addlist.size-1,1;,第二条语句中的list.size为1,因此将另一个1值放入arraylist的元素0中。size返回arraylist中的元素数,而不是其容量。您可能只想使用普通的旧addvalue,而不是addindex,value。
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
[1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1]