I'm trying to add a String before a specific element in my LinkedList ArrayList. I'm trying to shift every element from the specific index to the right in order to insert the new element. Can elements not be shifted in an arrayList? I receive the following error:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
Here is my insertBefore method:
public static void insertBefore() {
System.out.println("Which song would you like to add: ");
element = scanner.next();
for(int i=linkedList.size(); i>index; i--){
linkedList.set(index+1, linkedList.get(index));
}
linkedList.add(index, element);
}
This is my 5th attempt after trying out much simpler methods. Thanks for your answers in advance!