-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListDemo.java
More file actions
66 lines (42 loc) · 1.83 KB
/
ArrayListDemo.java
File metadata and controls
66 lines (42 loc) · 1.83 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.ArrayList;
public class ArrayListDemo
{
public static void main(String[] args)
{
ArrayList people = new ArrayList(); // instantiate an ArrayList object
people.add ("Brian"); // no wrapper class needed
people.add ("Leeroy"); // since Strings are Objects
people.add ("Susan");
people.add ("Annie");
people.add(new Integer(42)); // wrapper class needed to add an int
System.out.println(people);
System.out.println("\nSize of the array: "+people.size());
int x = ((Integer)people.get(4)).intValue();
System.out.println("\nThe number " + x + " is also in the list");
String a = (String)people.get(2); // convert the Object into a String
a=a.toUpperCase();
System.out.println("\nThe 3rd person's name in caps is " + a);
int location = people.indexOf("Leeroy"); //finds where 'leeroy' is located
people.remove(location);
System.out.println("\n\nAfter removing Leeroy:");
System.out.println(people);
System.out.println("\nSize of the array: "+people.size());
System.out.println("\nAt index #1: " + people.get(1));
people.set (1, "Jen"); // replace index 1 with Jen
System.out.println("\n\nSusan replaced by Jen:");
System.out.println(people);
System.out.print("\nWhich person shall we add at index #2? ");
String b = SavitchIn.readLine();
people.add(2,b);
System.out.println(people);
System.out.println("\nSize of the array: "+people.size());
System.out.println("\npeople printed on separate lines:");
for (int i=0; i<people.size(); i++)
System.out.println(people.get(i));
people.add(new Integer(99)); // Integer wrapper class
System.out.println("\nA number added to the list:");
System.out.println(people);
/* */
System.out.println();
}
}