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);
}
}
}
第六天Java实训
于 2024-01-29 19:10:34 首次发布