-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListDemo.java
More file actions
34 lines (29 loc) · 1.11 KB
/
ArrayListDemo.java
File metadata and controls
34 lines (29 loc) · 1.11 KB
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.*;
// ArrayList used ideally to search not add edit
// when adding & editing ideally you should use linked list
public class ArrayListDemo {
public static void main (String args []) {
ArrayList<String> a = new ArrayList<String>();
// add the items to the array list
a.add("Charles");
a.add("Paul");
a.add("Marica");
a.add("Joe");
// remove one of them
a.remove(1);
System.out.println("Marica found in position "+ a.indexOf( "Marica" ) );
// display the list using an iterator
//iterator an object pointing to the head of the list
// hasNext(boolean) a method within iterator
// while not null checked by hasNext
// next point to the next object
// delete to delete an object
// Iterator is a class (interface)
// iterator a method that returns an object of
// type iterator pointing to the head of the list
Iterator i = a.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
}
}