|
| 1 | +���ϵIJ������裺 |
| 2 | + A:�������϶��� |
| 3 | + B:����Ԫ�ض��� |
| 4 | + C:��Ԫ�����ӵ����� |
| 5 | + D:�������� |
| 6 | + |
| 7 | +���ϵı�����ʽ�� |
| 8 | + Collection:������,��ǿfor |
| 9 | + |--List:��ͨfor |
| 10 | + |--Set: |
| 11 | + |
| 12 | +�ַ���(���� hello,world,java) |
| 13 | +�Զ������(ѧ����) |
| 14 | + class Student { |
| 15 | + private String name; |
| 16 | + private int age; |
| 17 | + |
| 18 | + public Student() {} |
| 19 | + |
| 20 | + public Student(String name,int age) { |
| 21 | + this.name = name; |
| 22 | + this.age = age; |
| 23 | + } |
| 24 | + |
| 25 | + public void setName(String name) { |
| 26 | + this.name = name; |
| 27 | + } |
| 28 | + |
| 29 | + public String getName() { |
| 30 | + return name; |
| 31 | + } |
| 32 | + |
| 33 | + public void setAge(int age) { |
| 34 | + this.age = age; |
| 35 | + } |
| 36 | + |
| 37 | + public int getAge() { |
| 38 | + return age; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | +1��Collection |
| 43 | + A:�ַ��� |
| 44 | + Collection<String> c = new ArrayList<String>(); |
| 45 | + |
| 46 | + c.add("hello"); |
| 47 | + c.add("world"); |
| 48 | + c.add("java"); |
| 49 | + |
| 50 | + Iterator<String> it = c.iterator(); |
| 51 | + while(it.hasNext()) { |
| 52 | + String s = it.next(); |
| 53 | + System.out.println(s); |
| 54 | + } |
| 55 | + |
| 56 | + for(String s : c) { |
| 57 | + System.out.println(s); |
| 58 | + } |
| 59 | + |
| 60 | + B:�Զ������ |
| 61 | + Collection<Student> c = new ArrayList<Student>(); |
| 62 | + |
| 63 | + Student s1 = new Student("������",50); |
| 64 | + Student s2 = new Student("����",40); |
| 65 | + Student s3 = new Student("�⾩",30); |
| 66 | + |
| 67 | + c.add(s1); |
| 68 | + c.add(s2); |
| 69 | + c.add(s3); |
| 70 | + |
| 71 | + Iterator<Student> it = c.iterator(); |
| 72 | + while(it.hasNext()) { |
| 73 | + Student s = it.next(); |
| 74 | + System.out.println(s.getName()+"---"+s.getAge()); |
| 75 | + } |
| 76 | + |
| 77 | + for(Student s : c) { |
| 78 | + System.out.println(s.getName()+"---"+s.getAge()); |
| 79 | + } |
| 80 | + |
| 81 | +2��List |
| 82 | + A:�ַ��� |
| 83 | + List<String> list = new ArrayList<String>(); |
| 84 | + |
| 85 | + list.add("hello"); |
| 86 | + list.add("world"); |
| 87 | + list.add("java"); |
| 88 | + |
| 89 | + Iterator<String> it = list.iterator(); |
| 90 | + while(it.hasNext()) { |
| 91 | + String s = it.next(); |
| 92 | + System.out.println(s); |
| 93 | + } |
| 94 | + |
| 95 | + for(String s : list) { |
| 96 | + System.out.println(s); |
| 97 | + } |
| 98 | + |
| 99 | + for(int x=0; x<list.size(); x++) { |
| 100 | + String s = list.get(x); |
| 101 | + System.out.println(s); |
| 102 | + } |
| 103 | + |
| 104 | + B:�Զ������ |
| 105 | + List<Student> list = new ArrayList<Student>(); |
| 106 | + |
| 107 | + Student s1 = new Student("������",50); |
| 108 | + Student s2 = new Student("����",40); |
| 109 | + Student s3 = new Student("�⾩",30); |
| 110 | + |
| 111 | + list.add(s1); |
| 112 | + list.add(s2); |
| 113 | + list.add(s3); |
| 114 | + |
| 115 | + Iterator<Student> it = list.iterator(); |
| 116 | + while(it.hasNext()) { |
| 117 | + Student s = it.next(); |
| 118 | + System.out.println(s.getName()+"---"+s.getAge()); |
| 119 | + } |
| 120 | + |
| 121 | + for(Student s : list) { |
| 122 | + System.out.println(s.getName()+"---"+s.getAge()); |
| 123 | + } |
| 124 | + |
| 125 | + for(int x=0; x<list.size(); x++) { |
| 126 | + Student s = list.get(x); |
| 127 | + System.out.println(s.getName()+"---"+s.getAge()); |
| 128 | + } |
| 129 | + |
| 130 | +3��ArrayList(���� �Լ�����) |
| 131 | + A:�ַ��� |
| 132 | + ArrayList<String> array = new ArrayList<String>(); |
| 133 | + |
| 134 | + array.add("java"); |
| 135 | + array.add("hello"); |
| 136 | + array.add("world"); |
| 137 | + |
| 138 | + //��ͨforѭ�� |
| 139 | + for(int i =0; i<array.size(); i++) { |
| 140 | + String s = array.get(i); |
| 141 | + System.out.println(s); |
| 142 | + } |
| 143 | + |
| 144 | + //��ǿfor |
| 145 | + for(String s : array) { |
| 146 | + System.out.println(s); |
| 147 | + } |
| 148 | + |
| 149 | + //������ |
| 150 | + Iterator<String> it = array.iterator(); |
| 151 | + while(it.hasNext()) { |
| 152 | + String s = it.next(); |
| 153 | + System.out.println(s); |
| 154 | + } |
| 155 | + B:�Զ������ |
| 156 | + ArrayList<Student> array = new ArrayList<Student>(); |
| 157 | + |
| 158 | + Student s1 = new Student("laozi",1000); |
| 159 | + Student s2 = new Student("kongzi",100); |
| 160 | + Student s3 = new Student("zhuangzi",10); |
| 161 | + |
| 162 | + array.add(s1); |
| 163 | + array.add(s2); |
| 164 | + array.add(s3); |
| 165 | + |
| 166 | + //��ͨforѭ�� |
| 167 | + for (int i = 0; i<array.size(); i++) { |
| 168 | + Student s = array.get(i); |
| 169 | + System.out.println(s.getName()+"---"+s.getAge()); |
| 170 | + } |
| 171 | + |
| 172 | + //��ǿforѭ�� |
| 173 | + for (Student s : array) { |
| 174 | + System.out.println(s.getName()+"---"+s.getAge()); |
| 175 | + } |
| 176 | + |
| 177 | + //������ѭ�� |
| 178 | + for(Iterator<Student> it = array.iterator(); it.hasNext();) { |
| 179 | + Student s = it.next(); |
| 180 | + System.out.println(s.getName()+"---"+s.getAge()); |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | +4��Vector(�Լ�����) |
| 185 | +A:�ַ��� |
| 186 | + Vector<String> vector = new Voctor<String>(); |
| 187 | + |
| 188 | + vector.add("java"); |
| 189 | + vector.add("hello"); |
| 190 | + vector.add("world"); |
| 191 | + |
| 192 | + //��ͨforѭ�� |
| 193 | + for(int i =0; i<vector.size(); i++) { |
| 194 | + String s = vector.get(i); |
| 195 | + System.out.println(s); |
| 196 | + } |
| 197 | + |
| 198 | + //��ǿfor |
| 199 | + for(String s : vector) { |
| 200 | + System.out.println(s); |
| 201 | + } |
| 202 | + |
| 203 | + //������ |
| 204 | + Iterator<String> it = vector.iterator(); |
| 205 | + while(it.hasNext()) { |
| 206 | + String s = it.next(); |
| 207 | + System.out.println(s); |
| 208 | + } |
| 209 | + B:�Զ������ |
| 210 | + vectorList<Student> array = new vectorList<Student>(); |
| 211 | + |
| 212 | + Student s1 = new Student("laozi",1000); |
| 213 | + Student s2 = new Student("kongzi",100); |
| 214 | + Student s3 = new Student("zhuangzi",10); |
| 215 | + |
| 216 | + vector.add(s1); |
| 217 | + vector.add(s2); |
| 218 | + vector.add(s3); |
| 219 | + |
| 220 | + //��ͨforѭ�� |
| 221 | + for (int i = 0; i<vector.size(); i++) { |
| 222 | + Student s = vector.get(i); |
| 223 | + System.out.println(s.getName()+"---"+s.getAge()); |
| 224 | + } |
| 225 | + |
| 226 | + //��ǿforѭ�� |
| 227 | + for (Student s : vector) { |
| 228 | + System.out.println(s.getName()+"---"+s.getAge()); |
| 229 | + } |
| 230 | + |
| 231 | + //������ѭ�� |
| 232 | + for(Iterator<Student> it = vector.iterator(); it.hasNext();) { |
| 233 | + Student s = it.next(); |
| 234 | + System.out.println(s.getName()+"---"+s.getAge()); |
| 235 | + } |
| 236 | + |
| 237 | +5��LinkedList(���� �Լ�����) |
| 238 | + |
| 239 | +6��Set |
| 240 | + A:�ַ��� |
| 241 | + Set<String> set = new HashSet<String>(); |
| 242 | + |
| 243 | + set.add("hello"); |
| 244 | + set.add("world"); |
| 245 | + set.add("java"); |
| 246 | + |
| 247 | + Iterator<String> it = set.iterator(); |
| 248 | + while(it.hasNext()) { |
| 249 | + String s = it.next(); |
| 250 | + System.out.println(s); |
| 251 | + } |
| 252 | + |
| 253 | + for(String s : set) { |
| 254 | + System.out.println(s); |
| 255 | + } |
| 256 | + |
| 257 | + B:�Զ������ |
| 258 | + Set<Student> set = new HashSet<Student>(); |
| 259 | + |
| 260 | + Student s1 = new Student("������",50); |
| 261 | + Student s2 = new Student("����",40); |
| 262 | + Student s3 = new Student("�⾩",30); |
| 263 | + |
| 264 | + set.add(s1); |
| 265 | + set.add(s2); |
| 266 | + set.add(s3); |
| 267 | + |
| 268 | + Iterator<Student> it = set.iterator(); |
| 269 | + while(it.hasNext()) { |
| 270 | + Student s = it.next(); |
| 271 | + System.out.println(s.getName()+"---"+s.getAge()); |
| 272 | + } |
| 273 | + |
| 274 | + for(Student s : set) { |
| 275 | + System.out.println(s.getName()+"---"+s.getAge()); |
| 276 | + } |
| 277 | + |
| 278 | +7��HashSet(���� �Լ�����) |
| 279 | + �洢�Զ�������ʱ�������Ա����ֵ��ͬ�����Ǿ���Ϊ��ͬһ��Ԫ�ء� |
| 280 | + |
| 281 | +8��TreeSet(���� �Լ�����) |
| 282 | + �洢�Զ�������ʱ�������Ա����ֵ��ͬ�����Ǿ���Ϊ��ͬһ��Ԫ�ء� |
| 283 | + ���ȸ��������С������������ڸ��������ij������� |
0 commit comments