Java ArrayList add Example
ArrayList class add method example. This example shows you how to use add method.
ArrayList class add method example. public void add(int index, List element) Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
Here is the code
/**
* @ # Add1.java
* A class repersenting use to add(int index,E element)
* method of ArrayList class in java.util package
* version 14 May 2008
* author Rose India
*/
import java.util.*;
public class Add1 {
public static void main(String args[]) {
List arrlist = new ArrayList();
int i = 1;
String str = "Rose India";
//Add the specified element to the end of this list.
arrlist.add(i);
arrlist.add(str);
System.out.println("arrlist = " + arrlist);
//Add specified element at the specified position in this list.
arrlist.add(1,11);
System.out.println("arrlist = " + arrlist);
}
} |
Output
arrlist = [1, Rose India]
arrlist = [1, 11, Rose India] |
|