实现的简单的例子
一个数组和一个散列表!
package com;
import java.util.*;
public class My_collection {
public static void main(String[] args) {
ArrayList a1 = new ArrayList();
a1.add(new Point(1,1));
a1.add(new Point(2,2));
a1.add(new Point(3,3));
/*
for(int i=0; i<a1.size(); i++) {
System.out.println(a1.get(i));
}*/
Iterator itr = a1.iterator();
while (itr.hasNext()){
System.out.println(itr.next());
}
//获取对象数组
Object[] objs = a1.toArray();
for (int i=0; i< objs.length;i++) {
System.out.println(objs[i]);
}
System.out.println(objs);
//返回一个列表
List li = Arrays.asList(objs);
System.out.println(li);
//散列表
HashSet Hset = new HashSet();
Hset.add("a");
Hset.add(new Student(3, "mingming"));
Hset.add(new Student(2,"宜信"));
Hset.add(new Student(1,"XIAOJU"));
Iterator it = Hset.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
}
class Point {
int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
public String toString(){
return "x=" +x+ ",y=" +y;
}
}
class Student {
int num;
String name;
Student(int num ,String name){
this.num = num;
this.name = name;
}
public int HashCode(){
return num*name.hashCode();
}
public boolean equals(Object o){
Student S =(Student) o;
return num == S.num && name.equals(S.name);
}
public String toString() {
return "name :=" + num + name;
}
}
打印出来的结果如下:
x=1,y=1
x=2,y=2
x=3,y=3
x=1,y=1
x=2,y=2
x=3,y=3
[Ljava.lang.Object;@6e0be858
[x=1,y=1, x=2,y=2, x=3,y=3]
a
name :=3mingming
name :=2宜信
name :=1XIAOJU
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!!!~~~~~~~~~~~~~~~~~~~~~~~