Java 容器篇:Collection 下的Set和List的基本认识和用法

Collection Set和List

今天讨论Java的Set和List的基本用法。
首先说一下List
List是重复且有序的一个容器。
collection List的代码

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;

public class collection {
    public  static void main(String[] args)
    {
        Collection c=new ArrayList();//没有加泛型的,则没有限制它的数据类型
        Collection<String > cc=new ArrayList <>();
        Collection  shuju=new ArrayList();//List 是有序且重复的
        Scanner scanner=new Scanner(System.in);
        c.add("牛逼");
        c.add(1);
        c.add(1511);
        c.add(1);
        Iterator iterator =c.iterator();
        while (iterator.hasNext())
        {
            Object ob=iterator.next();
            System.out.println(ob);
        }
        cc.add("s");
        System.out.println(c);
        System.out.println("========");
        System.out.println(cc);
        c.remove(1);
        System.out.println("移除后的c"+c);
        //注:容器的里面存取的是元素的地址,而不是元素本身,remove移除的是存取在容器的元素地址,而不是元素本身
//        c.clear();
//        System.out.println(c);
        Object [] objects=new Object[3];
        objects=c.toArray();
        System.out.println(objects);
        System.out.println(c.contains(1511));//可以多查查Java帮助文档
        System.out.println("请输入");
        shuju.add(scanner.nextInt());//也可以这么用
        System.out.println("存取结果为"+shuju);
    }
}

解析:

Collection<String > cc=new ArrayList <>();

这里加了个泛型实际上是限定传进去的数据类型,<数据类型>这里可以定义你想要的数据类型,但是例如int类型,就不能直接int类型了。具体的解析可以去看看Java的帮助文档,我这里是比较通俗的理解。

c.add("牛逼");

这里是添加元素地址的。
然后我们可以使用迭代器(迭代器的具体内容到后面会出一个博客专门解释,这次只说简单的使用)

Iterator iterator =c.iterator();//获取目标容器的迭代
        while (iterator.hasNext())//判断目标容器是否还有可迭代的东西,若有,则返回true
        {
            Object ob=iterator.next();//获取下一个迭代的东西
            System.out.println(ob);
        }

当然,你一可以这样简单的输出

System.out.println(c);

这里需要注意一下,remove并不是移除List里面的元素。容器List里面存放的是元素的地址,而不是元素本身,remove移除的是存入其中的地址,但是元素本身还在。

c.remove(1);//移除目标元素的地址

当然,你也可以用toArray();将其转换成Object的数组,然后输出。

Object [] objects=new Object[3];
        objects=c.toArray();
        System.out.println(objects);

+++++++++++++++++++++++++++++++++++++++++++++++++++++List基本使用完结

Set的使用

不说多,先上代码

import java.util.*;

public class Colletion_set {
    public static void main(String[] args)
    {
        Scanner scanner=new Scanner(System.in);
        HashSet hashSet=new HashSet();//与LinkedHashSet一样,无序而不重复,Set和List都属于Colletion下的,所以有序
        Collection c=new LinkedHashSet();//LinkedHashSet 有序而不重复的
        c.add(1);
        c.add(1);
        c.add(2);
        for (int i=0;i<4;i++)
        {
            c.add(scanner.next());//这里的scaner.next()是可以输入任何类型的数据,区别一下类似.nextInt();
        }
        System.out.println(c);
        hashSet.add(1511);
        hashSet.add(1511);
        hashSet.add(5858);
        System.out.println("hashSet:"+hashSet);
        System.out.println(hashSet.contains(1511));//具体功能看帮助文档
        System.out.println("请开始为HashSet:");
        for (int j=0;j<4;j++)
        {
            hashSet.add(scanner.next());
        }
        System.out.println(hashSet);
        Iterator iterator=hashSet.iterator();//迭代器的用法   这里先使用最初的迭代器,具体写法后面再详解
        while (iterator.hasNext())//如果是true,那么他会继续迭代更多的元素
        {
            Object ss=iterator.next();//迭代下一个元素
            System.out.println(ss);//输出元素
        }
    }
}

具体详解在代码注释中,它的用法跟List相差不大,不过具体用法还要参考帮助文档。
好啦,Java容器篇开篇就到这了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值