package com.java.collectiontest;
/*
* @Author jingxurun
* @Date 2024/1/29 星期一 8:44
* @Version
**/
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
public class CollectionTest {
@Test
public void collectionTest(){
//接口类型的引用指向实现的对象,形成多态
Collection collection = new ArrayList();
System.out.println(collection);//[]
//向集合中添加元素,该方法的形参要求是Obiect类型
boolean b = collection.add("one");//[one]
System.out.println(collection);//[one]
System.out.println("b = " + b);//true
collection.add(2);//[one,2]
System.out.println(collection);//[one,2]
collection.add('a');
collection.add(3.14);
collection.add("jxr");
System.out.println(collection);//[one.2.a.3.14,jxr]
//判断是否包含对象
b = collection.contains("two");
System.out.println("b = " + b);//false
b = collection.contains("one");
System.out.println("b = " + b);//true
//判断集合是否为空
b = collection.isEmpty();
System.out.println("b = " + b);//false
System.out.println("集合中的元素有" + collection);
b = collection.remove("one");
System.out.println("b = " + b);//true
System.out.println("集合中的元素有" + collection);
//将集合转换成数组
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);
//迭代器,遍历集合中的元素(二)
Iterator it = objects1.iterator();//Iterator对象称为迭代器
/*
*b = it.hasNext();
*System.out.println("b = " + b);//true 如果仍有元素可以迭代,则返回true
*Object obj = it.next();
*System.out.println("元素:" + obj);//返回迭代的下一个元素
**/
while (it.hasNext() == true){
Object obj = it.next();
System.out.println(obj);
}
//遍历集合中的元素(三)
for (Object o : objects1 ) {
System.out.println(o);
}
}
/**
* list集合常用的方法
*/
@Test
public void listTest() {
List list = new ArrayList();
int size = list.size();
System.out.println(size);//0
System.out.println(list);//[]
//向集合中添加元素
list.add(0,"one");
list.add(1,2);
list.add(2,'3');
list.add(3,3.0);
System.out.println(list);
List list1 = new ArrayList();
list1.add("two");
list1.add(2);
list1.add(2.0);
list1.addAll(list);
System.out.println(list1);
//根据下标查看集合中指定位置的元素
Object o = list.get(3);
System.out.println(o);
//根据下标修改集合中的指定位置的元素
o = list.set(0,1.0);
System.out.println("下表为0的改为" + o);
System.out.println(list);
//根据下标删除集合指定位置的元素
o = list.remove(0);
System.out.println("删除的元素为" + o);
System.out.println(list);
//获取集合(前闭后开)
List list2 = list.subList(0,3);
System.out.println(list);
System.out.println("子集合为:" + list2);
list.remove(0);
System.out.println(list);
//System.out.println("子集合为:" + list2);//无法打印
}
@Test
public void collectionTest02(){
Collection<String> collection = new ArrayList<>();
collection.add("AA");
collection.add("BB");
collection.add("CC");
collection.add("DD");
collection.add("EE");
System.out.println(collection);
}
}
package com.java.queueDemo;/*
* @Author jingxurun
* @Date 2024/1/29 星期一 17:19
* @Version
**/
import org.junit.Test;
import java.util.Queue;
import java.util.LinkedList;
public class QueueTest {
@Test
public void queueTest(){
//准备一个Queue集合
Queue<Integer> queue = new LinkedList<>();
//将数据11.22.33.44.55依次入队并打印
for (int i = 0; i < 5; i++) {
queue.offer(i * 11);
System.out.println(queue);
}
//然后查看队首元素并打印
Integer it = queue.peek();
System.out.println("队首元素为:" + it);
//然后将队列中所有数据依次出队并打印
//poll
for (int i = 0; i < queue.size(); i++) {//不行,因为队列长度会变
Integer it1 = queue.poll();
System.out.println(it1);
}
//正确做法
int len = queue.size();
for (int i = 0; i < len; i++) {
Integer it1 = queue.poll();
System.out.println(it1);
}
}
}