-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathListApp.java
More file actions
68 lines (49 loc) · 1.72 KB
/
ListApp.java
File metadata and controls
68 lines (49 loc) · 1.72 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
67
68
package com.hmkcode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import com.hmkcode.vo.Person;
public class ListApp
{
public static void main( String[] args )
{
// ( 1 ) List
// A. Initiate
List<Person> arrayList = new ArrayList<Person>();
List<Person> linkedList = new LinkedList<Person>();
// B. Populate
for(Person person:getPersons()){
arrayList.add(person);
linkedList.add(person);
}
// --> print
System.out.println("--------- Print All -------------");
System.out.println("ArrayList: "+arrayList);
System.out.println("LinkedList: "+linkedList);
// C. Iterate
System.out.println("--------- Print Iterate by index -------------");
for(int i = 0; i < linkedList.size(); i++){
System.out.println("LinkedList[ "+i+" ]"+linkedList.get(i));
}
System.out.println("--------- Print Iterate by for each -------------");
//this is an efficient way to iterate
for(Person person:linkedList){
System.out.println("LinkedList: "+person);
}
// D. Sort
Collections.sort(linkedList);
// --> print
System.out.println("--------- Print Sorted List -------------");
System.out.println("Sorted LinkedList: "+linkedList);
}
private static Person[] getPersons(){
Person[] persons = new Person[5];
persons[0] = new Person("Brit", 29);
persons[1] = new Person("John", 32);
persons[2] = new Person("Jack", 27);
persons[3] = new Person("Jenifer", 24);
persons[4] = new Person("Brit", 37);
return persons;
}
}