集合(一)
1.Collection集合
package com.java;
import java.util.*;
/**
* @author WuYazhen
* @Date 2024/1/29 8:43
*/
public class CollectionTest {
public static void main(String[] args) {
Collection collection = new ArrayList();
System.out.println(collection);
boolean b = collection.add("one");
System.out.println(collection);
System.out.println("b = "+b);
collection.add(2);
collection.add("a");
collection.add(3.14);
collection.add("wyz");
System.out.println(collection);
//判断集合是否包含元素
b = collection.contains(1);
System.out.println("b = " + b);
b = collection.contains("one");
System.out.println("b = " + b);
//判断集合是否为空
System.out.println("===========================");
b = collection.isEmpty();
System.out.println("b = " + b);
System.out.println("集合中的元素" + collection);
//删除某元素
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]);
}
//将数组转换成集合
System.out.println("===========================");
List objects1 = Arrays.asList(objects);
System.out.println(objects1);
//迭代器遍历集合中的元素
System.out.println("===========================");
Iterator it = objects1.iterator();
while (it.hasNext()){
Object obj = it.next();
System.out.println(obj);
}
//遍历集合中的元素,方法三
System.out.println("===========================");
for (Object o : objects1){
System.out.println(o);
}
}
}
2.List集合
package com.java;
import org.junit.Test;
import java.util.*;
/**
* @author WuYazhen
* @Date 2024/1/29 11:07
*/
public class ListTest {
public static void main(String[] args) {
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);
System.out.println("===========================");
//添加所有元素:
List list1 = new ArrayList();
list1.add(0,"two");
list1.add(1,10);
list.addAll(list1);
System.out.println(list);
System.out.println("===========================");
//查找:
System.out.println(list.get(0));
System.out.println("===========================");
//获取子List(前开后闭)
System.out.println(list.subList(1,3));
System.out.println("===========================");
//修改:
list.set(1,1);
System.out.println(list);
System.out.println("===========================");
//删除(根据下标删除):
list.remove(0);
System.out.println(list);
System.out.println("===========================");
//删除(根据元素删除):
list.remove("two");
System.out.println(list);
}
}
作业:
import java.util.Stack;
/**
* @author WuYazhen
* @Date 2024/1/29 16:56
*/
public class StackTest {
public static void main(String[] args) {
// 创建一个新的Stack实例
Stack<Integer> stack = new Stack<>();
// 将元素11、22、33、44、55依次入栈
stack.push(11);
stack.push(22);
stack.push(33);
stack.push(44);
stack.push(55);
// 打印当前栈的内容
System.out.println("栈中的元素: " + stack);
// 查看并打印栈顶元素
System.out.println("栈顶元素: " + stack.peek());
// 依次出栈并打印元素
while (!stack.isEmpty()) {
System.out.println("出栈的元素: " + stack.pop());
}
}
}
3.泛型
package com.java;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author WuYazhen
* @Date 2024/1/29 16:50
*/
public class CollectionTest02 {
public static void main(String[] args) {
Collection<String> collection = new ArrayList();
collection.add("aa");
collection.add("bb");
collection.add("cc");
System.out.println(collection);
}
}
4.Queue集合
package com.java;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author WuYazhen
* @Date 2024/1/29 17:19
* 将11 22 33 44 55依次入队并打印
* 然后查看队首元素并打印
* 将对中元素依次出队,并打印
*/
public class QueueTest {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
//打印队列
for (int i = 1;i<=5;i++){
queue.offer(i*11);
System.out.println(queue);
}
//队首元素
System.out.println("队首元素:" + queue.peek());
//出队列
int len = queue.size();
for (int i = 1;i <= len;i++){
Integer i1 = queue.poll();
System.out.println(i1);
}
}
}