Biblioteca Java - Blame information for rev 2
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mihai | 1 | package colectii.liste; |
2 | import java.util.*; | ||
3 | public class RemoveExample { | ||
4 | static void displayAll(List l){ | ||
5 | System.out.println("Display all persons."); | ||
6 | for (Object p : l) { | ||
7 | System.out.println(p); | ||
8 | } | ||
9 | } | ||
10 | |||
11 | public static void main(String[] args) { | ||
12 | List c = new ArrayList(); | ||
13 | Person p1 = new Person("aaa","bbb"); | ||
14 | Person p2 = new Person("ccc","ddd"); | ||
15 | Person p3 = new Person("xxx","yyy"); | ||
16 | Person p4 = new Person("zzz","qqq"); | ||
17 | |||
18 | c.add(p1);c.add(p2);c.add(p3);c.add(p4); | ||
19 | displayAll(c); | ||
20 | c.remove(p2); | ||
21 | displayAll(c); | ||
22 | |||
23 | Person p5 = new Person("aaa","bbb"); | ||
24 | c.remove(p5); | ||
25 | displayAll(c); | ||
26 | |||
27 | c.remove(0); | ||
28 | displayAll(c); | ||
29 | } | ||
30 | } | ||
31 | |||
32 | class Person{ | ||
33 | String firstname; | ||
34 | String lastname; | ||
35 | Person(String f, String l){ | ||
36 | this.firstname = f;this.lastname = l; | ||
37 | } | ||
38 | |||
39 | public boolean equals(Object obj) { | ||
40 | if(obj instanceof Person){ | ||
41 | Person p = (Person)obj; | ||
42 | return p.firstname.equalsIgnoreCase(firstname)&&p.lastname.equalsIgnoreCase(lastname); | ||
43 | } | ||
44 | else return false; | ||
45 | } | ||
46 | |||
47 | public String toString() { | ||
48 | return "persoana:"+firstname+":"+lastname; | ||
49 | } | ||
50 | } |