//Insert a value by using arrayName.add(index, value)
//Will not remove current element at index. Will simply move all elements to
//the right.
import java.util.ArrayList;
public class main{
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0);
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(0, 100);
//enhanced for loop will print all elements of the ArrayList
for(int element : list){
System.out.print(list + ", ");
}
//Expected output: "100, 0, 1, 2, 3, 4, 5, "
}
}
/* add(index, item to add); */
ArrayList<String> languages = new ArrayList<String>();
languages.add("PHP");
languages.add("JAVA");
languages.add("C#");
languages.add(2, "RUBY"); /* at index 2, add RUBY*/
ArrayList<int> numbers = new ArrayList<int>();
numbers.add(9);
numbers.add(14);
numbers.add(2);
numbers.add(1, 15); /* at index 1, add 15*/
/*
After Execution:
languages => PHP JAVA RUBY C#
numbers => 9 15 14 2
*/
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// create ArrayList
ArrayList<String> Websites = new ArrayList<>();
// add() method without the index parameter
Websites.add("Softhunt.net");
Websites.add("Pythonprogramming.com");
Websites.add("Tutorial.io");
// add JavaScript.com at index 1
Websites.add(0, "JavaScript.com");
// add C++.net at index 3
Websites.add(4, "C++.net");
System.out.println("ArrayList: " + Websites);
}
}
arrlist.add(2,25);