java arraylist ensureCapacity not working

后端 未结 8 2019
天命终不由人
天命终不由人 2020-12-03 05:18

Either I\'m doing this wrong or i\'m not understanding how this method works.

ArrayList a = new ArrayList();
a.ensureCapacity(200         


        
8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 06:03

      public static void fillArrayList(ArrayList arrayList, long size) {
        for (int i = 0; i < size + 1; i++) {
          arrayList.add(i,"-1");
        }
      }
    
    public static void main(String[] args) throws Exception {
      ArrayList a = new ArrayList(10);
      fillArrayList(a, 190);
      a.add(190,"test");
      System.out.println(a.get(190).toString());
    }
    

提交回复
热议问题