ArrayList get/set element
In this chapter you will learn:
Get element by index
E get(int index)
Returns the element at the specified position in this list.
import java.util.ArrayList;
/*from j a v a 2 s. c o m*/
public class Main {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
al.add("C");
al.add("java2s.com");
al.add("D");
al.add("F");
al.add(1, "java2s.com");
System.out.println(al);
System.out.println(al.get(1));
}
}
The output:
Replace element by index
E set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
import java.util.ArrayList;
//from j a va2 s. c o m
public class Main {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
al.add("C");
al.add("java2s.com");
al.add("D");
al.add("F");
al.add(1, "java2s.com");
System.out.println(al);
System.out.println(al.set(1,"new java2s.com"));
System.out.println(al);
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Collections