-
Notifications
You must be signed in to change notification settings - Fork 92
/
list_1.java
45 lines (39 loc) · 1.22 KB
/
list_1.java
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
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
// HW -
// WAP a Java program which will create a list of int, string and decimal numbers using arraylist and linkedlists
public class list_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Array list for strings -
List<String> listStrings = new ArrayList<String>();
listStrings.add("One");
listStrings.add("Two");
listStrings.add("Three");
listStrings.add("Four");
System.out.println(listStrings);
System.out.println(listStrings.get(1));
listStrings.set(3, "Using set method!");
System.out.println(listStrings);
// Array list for integers -
List<Integer> listIntegers = new ArrayList<Integer>();
listIntegers.add(1);
listIntegers.add(2);
listIntegers.add(3);
listIntegers.add(4);
System.out.println(listIntegers);
// Iterating over a list -
Iterator<Integer> iter = listIntegers.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
// Linked List for numbers -
LinkedList<Number> numbers = new LinkedList<Number>();
numbers.add(10);
numbers.add(10.32);
numbers.add(45.3333333);
System.out.println(numbers);
}
}