ArrayList Implementation

ArrayList Implementation | Javamazon:

'via Blog this'


ArrayList Resizable-array implementation

ArrayList Resizable-array implementation of the List interface,Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.*;
public class IteratorDemo {
 public static void main(String args [])
 {
  // add elements to the array list
  ArrayList al = new ArrayList();
  al.add("A");
  al.add("B");
  al.add("C");
  al.add("D");
  al.add("E");
  // use the iterator to display contents of al
  Iterator itr=al.iterator();
  System.out.println("Contents of al Elements");
   while(itr.hasNext()){
    Object element = itr.next();
    System.out.println(element);
 
   }
  // modify objects being iterated over here
  System.out.println("Modified Contents of al Elements");
  ListIterator litr=al.listIterator();
   while(litr.hasNext()){
    Object element=litr.next();
    System.out.println("+" +element);
   }
   System.out.println("Reverse order of al Elements");
   while(litr.hasPrevious()){
    Object element=litr.previous();
    System.out.println("+" +element);
   }
 }
 
}
OUTPUT :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Contents of al Elements
A
B
C
D
E
Modified Contents of al Elements
+A
+B
+C
+D
+E
Reverse order of al Elements
+E
+D
+C
+B
+A

0 comments:

Post a Comment