This book is now obsolete Please use CSAwesome instead.
9.12. Getting and Setting Values in a List¶
You can get the object at an index using obj = listName.get(index)
and set the object at an index using listName.set(index,obj)
.
Note
Remember that you can get the value at an array index using value = arrayName[index]
. This is different from how you get the value from a list using obj = listName.get(index)
. You can set the value at an index in an array using arrayName[index] = value
, but with lists you use listName.set(index, object)
.
9.13. Removing an Object at an Index¶
You can also remove an object at an index in a list using remove(index)
which returns the removed object and shifts the remaining objects past the index left one index.
Note
The remove(int index)
method will remove the object at the index and shift left any values to the right of the current index. It doesn’t remove the object that matches the integer value given. In the example above it doesn’t remove the 1. It removes the 2 at index 1.
Check your understanding
- [1, 2, 3, 4, 5]
- The
set
will replace the item at index 2 so this can not be right. - [1, 2, 4, 5, 6]
- The
add
with an index of 2 and a value of 5 adds the 5 at index 2 not 3. Remember that the first index is 0. - [1, 2, 5, 4, 6]
- The
set
will change the item at index 2 to 4. The add of 5 at index 2 will move everything else to the right and insert 5. The lastadd
will be at the end of the list. - [1, 5, 2, 4, 6]
- The
add
with an index of 2 and a value of 5 adds the 5 at index 2 not 1. Remember that the first index is 0.
8-6-3: What will print when the following code executes?
List<Integer> list1 = new ArrayList<Integer>();
list1.add(new Integer(1));
list1.add(new Integer(2));
list1.add(new Integer(3));
list1.set(2, new Integer(4));
list1.add(2, new Integer(5));
list1.add(new Integer(6));
System.out.println(list1);
You can step through the code above by clicking on the following Example-8-6-1.
- ["Sarah", "Destini", "Layla", "Sharrie"]
- Remember that the first index is 0 not 1.
- ["Sarah", "Destini", "Anaya", "Layla", "Sharrie"]
set
changes the value and the first index is 0 not 1.- ["Anaya", "Sarah", "Sharrie"]
add
at index 1 adds the new value at that index but moves right any existing values.- ["Anaya", "Sarah", "Destini", "Sharrie"]
- The list is first ["Anaya", "Layla", "Sharrie"] and then changes to ["Anaya", Destini", "Sharrie"] and then to ["Anaya", "Sarah", "Destini", "Sharrie"]
8-6-4: What will print when the following code executes?
List<String> list1 = new ArrayList<String>();
list1.add("Anaya");
list1.add("Layla");
list1.add("Sharrie");
list1.set(1, "Destini");
list1.add(1, "Sarah");
System.out.println(list1);
You can step through the code above by clicking on the following Example-8-6-2.
- [1, 2, 3, 4, 5]
- The
set
will replace the 3 at index 2 so this isn't correct. - [1, 2, 4, 5, 6]
- The
add
with an index of 1 and a value of 5 adds the 5 at index 1 not 3. Remember that the first index is 0. - [1, 2, 5, 4, 6]
- The
set
will change the item at index 2 to 4. Theadd
of 5 at index 1 will move everything else to the right and insert 5. The lastadd
will be at the end of the list. - [1, 5, 2, 4, 6]
add
without an index adds at the end,set
will replace the item at that index,add
with an index will move all current values at that index or beyond to the right.
8-6-5: What will print when the following code executes?
List<Integer> numList = new ArrayList<Integer>();
numList.add(new Integer(1));
numList.add(new Integer(2));
numList.add(new Integer(3));
numList.set(2,new Integer(4));
numList.add(1, new Integer(5));
numList.add(new Integer(6));
System.out.println(numList);
You can step through the code above by clicking on the following Example-8-6-3.
- [2, 3]
- The
remove
will remove the item at the given index. - [1, 2, 3]
- The item at index 1 will be removed and all the other values shifted left.
- [1, 2]
- The 3 is at index 2. The item at index 1 will be removed.
- [1, 3]
- The item at index 1 is removed and the 3 is moved left.
8-6-6: What will print when the following code executes?
List<Integer> list1 = new ArrayList<Integer>();
list1.add(new Integer(1));
list1.add(new Integer(2));
list1.add(new Integer(3));
list1.remove(1);
System.out.println(list1);
You can step through the code above by clicking on the following Example-8-6-4.
- [2, 3]
- This would be true if it was
remove(0)
- [1, 2, 3]
- The
remove
will remove a value from the list, so this can't be correct. - [1, 2]
- The 3 (at index 2) is removed
- [1, 3]
- This would be true if it was
remove(1)
8-6-7: What will print when the following code executes?
List<Integer> list1 = new ArrayList<Integer>();
list1.add(new Integer(1));
list1.add(new Integer(2));
list1.add(new Integer(3));
list1.remove(2);
System.out.println(list1);
You can step through the code above by clicking on the following Example-8-6-5.