java=集合方法

实现的简单的例子

一个数组和一个散列表!

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!!!~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值