ArrayList Index Outside

Why does java.lang.IndexOutOfBoundsException occur in this example if the size of the ArrayList is predetermined? How to solve this problem?

 int size = 2: ArrayList<Integer[]> nums = new ArrayList<Integer[]>(size); Integer[] value1 = {1,2,3}; Integer[] value2 = {1,2}; nums.add(1,value1); // java.lang.IndexOutOfBoundsException nums.add(0,value2); 
+6
source share
7 answers

You cannot put an element in an ArrayList before another is set. If you want to do this, you will first have to assign null values ​​to the element in place of 0.

It will work if you do:

 int size = 2: ArrayList<Integer[]> nums = new ArrayList<Integer[]>(size); Integer[] value1 = {1,2,3}; Integer[] value2 = {1,2}; nums.add(0, null); nums.add(1,value1); nums.set(0,value2); 

edit / Replaced add by set to replace the null object

+11
source

The argument to the ArrayList constructor is not the size of the list, as your code suggests; this is the capacity of the underlying storage used by the data structure.

Capacity will increase as needed when you add items to the list. The only reason indicating the initial capacity in the constructor is to pre-allocate a large capacity if you know that you are going to add many elements. This means that the underlying array should not change too often when you add it.

No matter what value you specify in the ArrayList constructor, the size of the list is determined only by what you put in it, so you cannot get an element with index of 1 until you add at least 2 elements.

+8
source

While you add() in an ArrayList , the length of the list is 0. You cannot add minus the current end of the List .

I suggest doing:

 nums.add(value2); nums.add(value1); 

To get the order you need here,

+2
source

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#ArrayList(int )

The fact that you specified the initial capacity for it is only the initial size, which has an internal array, and which automatically increases if necessary. This has nothing to do with the position of the position , so your mistake. By the way, the default capacity for ArrayLists is 10, so you are actually reducing the default size.

+2
source

According to javadoc , when you write

 ArrayList<Integer[]> nums = new ArrayList<Integer[]>(size); 

You do not determine the number the size of the number, you determine the initial capacity . In this sense, ArrayLists are not like inline arrays (value1 and value2 in your code).

You can try it yourself: print the nums size after each line (the code is cleared to prevent an IndexOutOfBoundsException.

 int size = 2: ArrayList<Integer[]> nums = new ArrayList<Integer[]>(size); Integer[] value1 = {1,2,3}; Integer[] value2 = {1,2}; System.out.println(nums.size()); nums.add(value2); System.out.println(nums.size()); nums.add(value1); System.out.println(nums.size()); 

This will print:

 0 1 2 
+1
source

To get the result you want to get, you need to write

 nums.add(0, value1); nums.add(0, value2); 
0
source

To get the expected results, I would recommend using basic typed arrays like

 Integer[][] nums = new Integer[size][]; 

and then

 Integer[] value1 = {1,2,3}; Integer[] value2 = {1,2}; nums[1] = value1; nums[0] = value2; 

If you still need an ArrayList just use

 new ArrayList<Integer[]>(Arrays.asList(nums)); 
0
source

Source: https://habr.com/ru/post/905110/


All Articles