JAVA集合

java 集合

所有的集合都位于 java.util包下.

(容器类)集合应该具有的功能:

随意添加数据,不会溢出(自动扩容)
元素删除(任意位置,不考虑数据移动的问题)
元素添加
遍历集合
清空整个内容
获得元素的个数

集合的定义
Collection接口
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I78mFQPT-1573690591901)(./1568709723083.png)]
iterator类
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ABbw7UWa-1573690591903)(./1568709926524.png)]

由大到小,具体问题具体分析

课后问题1:java泛型

#集合练习

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

/**
 * @Author HuangTang
 * @Date 2019/9/17 - 16:25
 */
public class demo01 {
    public static void main(String[] args) {
        Collection c1=new ArrayList();
        c1.add("高数");
        c1.add("线代");
        c1.add("离散数学");
        System.out.println(c1);
        Collection c2=new ArrayList();
        c2.add("近代史");
        c2.add("形势与政策");
        c1.addAll(c2);
        System.out.println(c1);
        System.out.println(c2);
        c2.addAll(c1);
        System.out.println(c2);
//        c1.clear();
//        System.out.println(c1);
        //遍历集合,输出换行,输出每一个元素
        Iterator iterator=c1.iterator();//获取迭代器对象:iterator 无需new,用对象的iterator方法去调用
        while (iterator.hasNext()){
            //String小 需要向下转型
            String s = (String) iterator.next();
            System.out.println(s);
        }

    }
}

打印结果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XCyu4jH4-1573690591904)(./1568711427627.png)]

# list学习

import java.util.LinkedList;

/**
 * @Author HuangTang
 * @Date 2019/9/17 - 17:36
 */
public class demo03 {
    public static void main(String[] args) {
        LinkedList<String> linkedList=new LinkedList<String>();
        linkedList.add("1");
        for (int i=2;i<4;i++){
            linkedList.add(i+" ");
        }
        System.out.println(linkedList);
        //添加表头
        linkedList.addFirst("F");
        //添加表尾部
        linkedList.addLast("L");
        System.out.println(linkedList);
        //获取表头
        System.out.println(linkedList.getFirst());
        //获取结尾
        System.out.println(linkedList.getLast());
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0jJAy3ZX-1573690591905)(./1568713215144.png)]
//ArrayList和linkedlist
ArrayList基于数组
link基于链表

随机访问(查):
ArrayList>linkedlist
新增和删除
L>A
查找
无法确定

ArrayList有序的,可重复的。线程不太安全

字符串去重

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Author HuangTang
 * @Date 2019/9/17 - 17:46
 * 字符串去重
 */
public class demo04 {
    public static void main(String[] args) {
        ArrayList array=new ArrayList();
        array.add("hello0");
        array.add("hello1");
        array.add("hello2");
        array.add("hello3");
        array.add("hello4");
        array.add("hello5");
        array.add("hello6");
        array.add("hello7");
        array.add("hello8");
        array.add("hello9");
        array.add("hello10");
        array.add("hello1");
        ArrayList newarray=new ArrayList();
        Iterator it=array.iterator();
        while (it.hasNext()){
            String s=(String)it.next();
            System.out.print(s +" ");
            //字符串去重:新的集合包含该字符
            if(!(newarray.contains(s))){
                newarray.add(s);//添加到新的集合里
            }
        }
        System.out.println();
        for (int i =0;i<newarray.size();i++){
            System.out.print(newarray.get(i)+" ");
        }

    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kWTm6cPx-1573690591907)(./1568714393994.png)]

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值