package day06;
import org.junit.Test;
import java.util.*;
public class CollectionTest<collection> {
@Test
public void CollectionTest(){
//接口类型的引用指向实现的对象,形成多态
Collection collection=new ArrayList();
System.out.println(collection);//[]
//向集合中添加元素,该方法的形参要求是Object类型
boolean b=collection.add("one");
System.out.println(collection);//[one]
System.out.println("b="+b);//true
collection.add(2);
System.out.println(collection);//[one,2]
collection.add('a');
collection.add(3.14);
collection.add("小南");
System.out.println(collection);//[one,2,a,3.14,小南]
System.out.println("---------------");
//判断是否包含对象
b=collection.contains("two");
System.out.println("b="+b);
b=collection.contains("one");
System.out.println("b="+b);
//判断集合是否为空
b=collection.isEmpty();
System.out.println("b="+b);
System.out.println("------------------");
//删除元素
b=collection.remove("one");
System.out.println("b="+b);
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();
System.out.println(objects1);
System.out.println("--------");
//迭代器,遍历集合中的元素
Iterator it=objects1.iterator();
while (it.hasNext()==true){
Object obj=it.next();
System.out.println(obj);
}
System.out.println("--------------");
//使用增强for循环
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);
}
}
package day06;
import java.util.Stack;
public class StackTest {
public static void main(String[] args) {
Stack s1 = new Stack();
System.out.println("s1 = " + s1);
System.out.println("-----------------------------------------------");
for (int i = 1; i <= 5; i++) {
Object obj = s1.push(i * 11);
System.out.println("入栈的元素是:" + obj);
}
System.out.println("栈中的元素有:" + s1);
System.out.println("-----------------------------------------------");
Object obj2 = s1.peek();
System.out.println("获取到的栈顶元素是:" + obj2);
System.out.println("-----------------------------------------------");
int len = s1.size();
for (int i = 1; i <= len; i++) {
Object to = s1.pop();
System.out.println("出栈的元素是:" + to); // 55 44 33 22 11
}
System.out.println("-----------------------------------------------");
System.out.println("s1 = " + s1);
}
}
java实训第六天
于 2024-01-29 18:44:19 首次发布