第六天Java实训

import org.junit.Test;

import java.lang.reflect.Array;
import java.util.*;

public class collectionTest {
    @Test
    public void collectionTest(){
        //接口类型的引用指向实现的对象,行成多态
        Collection collection =new ArrayList();
        System.out.println(collection);//[]
        //向集合中添加元素,该方法的形参要求是Object类型
        boolean b =collection.add("one");//[one]
        System.out.println(collection);
        System.out.println("b =" + b);//true
        collection.add(2);//[one,2]
        System.out.println(collection);
        collection.add('a');//[one,2,a,3.14,张三]
        collection.add(3.14);
        collection.add("张三");
        System.out.println(collection);
        System.out.println("-------------------");
        b = collection.contains("tuo");
        System.out.println("b =" + b);//false
        b = collection.contains("one");
        System.out.println("b =" + b);//true

        //判断集合是否为空
        b = collection.isEmpty();
        System.out.println("b =" + b);
        System.out.println("====================");
        b = collection.remove("ome");//false
        System.out.println("集合中的元素有:" + collection);
        System.out.println("b =" + b);//true
        System.out.println("集合中的元素有:" + collection);
        System.out.println("======================");
        //将集合转换为数组
        Object[] objects = collection.toArray();
        //遍历数组中的元素
        for (int i=0;i <objects.length;i++){
            System.out.println(objects[i]);
        }
        //将数组转换为集合
        List objects1=Arrays.asList(objects);
        System.out.println(objects1);
        System.out.println("=================");
        Iterator iterator = objects1.iterator();
        /*b = iterator.hasNext();
        System.out.println("b =" + b);//ture
        Object object = iterator.next();
        System.out.println("元素:" + object);*/
        while (iterator.hasNext()==true){
            Object object = iterator.next();
            System.out.println(object);
        }
        System.out.println("~~~~~~~~~~~~~~~~");
        //遍历结合中的元素,方式三
        for (Object o:objects1){
            System.out.println(o);
        }
    }
    @Test
    public void listTest(){
        List list = new ArrayList();
        int size = list.size();
        System.out.println(size);
        System.out.println(list);
        list.add(0,"one");
        list.add(1,2);
        list.add(2,'3');
        list.add(3,3.14);
        System.out.println(list);
        List list1 = new ArrayList();
        list1.add("two");
        list1.add(10);
        list.addAll(list1);
        System.out.println(list);
        System.out.println("----------");
        Object o = list.get(3);
        System.out.println(o);
        o = list.set(0, 1);
        System.out.println("下表为0的位置修改为:" + o);
        System.out.println(list);

        o = list.remove(0);
        System.out.println("删除的元素为:" + o);
        System.out.println("=================");
        System.out.println(list);
        List list2 = list.subList(0,3);
        System.out.println("list集合:" + list);
        System.out.println("子集合中的元素" + list2);
        list2.remove(0);
        System.out.println("list集合:" + list);
        System.out.println("子集合中的元素" + list2);
    }



    @Test
    public void CollectionTest2(){
        Collection<String>collection = new ArrayList<>();
        collection.add("AA");
        collection.add("BB");
        collection.add("CC");
        collection.add("DD");
        System.out.println(collection);
    }
    @Test
    public void queueTest(){
        Queue<Integer> queue = new LinkedList<>();
        for (int i=1;i<=5;i++){
            queue.offer(i*11);
            System.out.println(queue);
        }
        Integer it = queue.peek();
        System.out.println("队首元素:" + it);
        System.out.println("------------");
        int len = queue.size();
        for (int i=1;i<len;i++){
            Integer it1 = queue.poll();
            System.out.println(it1);
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值