addOneToFront
In this problem, you're practicing how to add an element to the list at a specific index.
Complete the method addOneToFront
which adds the number 7 to the front of the list.
for example, if the list was [1 2 3]
then after calling this method it becomes [7 1 2 3]
.
Example: Adding to an ArrayList at an existing index or at the end of the list. Adding to an index that does not exist or is not the next open spot will result in an out-of-bounds exception.
ArrayList<Integer> myList = new ArrayList<Integer>();
myList.add(1, 5); //inserts 5 at index 1.
//There's a second version of add for lists in java!
void
add(int index,
E element): Inserts the specified element at the specified position in this
list.
The first one has 1 parameter which is the element you're adding to the end of the list.
The second one has 2 parameters. The first parameter is the index where you want to add the element and the second parameter is the element.
For exampleadd(5, 293)
means you're adding the element 293 at index 5.
The following are methods of a list that can be used to manipulate the contained data.