2022年03月23日讲到了集合的概念,同志们
1.List接口
也是一种集合
public class ListDemo {
public static void main(String[] args) {
//创建List集合,可以指定一个的长度,也可以不指定
List<Integer> list = new ArrayList<>();
// 1. add() 按照顺序添加元素
// 也可以添加到指定索引处
list.add(0, 123);
List<Integer> list1 = new ArrayList<>();
list1.add(120);
list1.add(220);
// 2. addAll() 将另一个集合的所有元素添加到本集合中
// 也可以添加到指定位置处
list.addAll(list1);
// 3. get() 拿到传入索引处的元素
System.out.println(list.get(2));
// 4. indexOf() 拿到传入的元素在该集合第一次出现位置的索引
// 5. lastIndexOf() 拿到传入的元素在该集合最后一次出现位置的索引
//如果没找到就返回-1
//也是通过对象的 equals() 方法判断对象是否相等
int i=list.indexOf(220);
System.out.println(i);
List<Student> students=new ArrayList<>();
students.add(new Student("李华旭",18));
students.add(new Student("杨志冰",14));
students.add(new Student("縢昱旸",12));
students.add(new Student("赵振宁",11));
int index=students.indexOf(new Student("赵振宁",11));
System.out.println(index);
// 6. remove() 通过索引或指定元素删除集合里的东西,
// 通过 equals() 方法判断是否已经被删除
// 如果在里面写一个 120 是会被直接当成索引的
// 想删除 120 这个数要写 Integer.valueOf(120)
list.remove(Integer.valueOf(120));
System.out.println(list);
// 7. set() 更改指定索引处的元素
list.set(0, 200);
System.out.println(list);
// 8. subList() 得到该集合从 fromIndex 到 endIndex 的所有元素
// 并拿到一个新集合
List<Integer> list2 = list.subList(0, 1);
System.out.println(list2);
List<String> strList = new ArrayList<>();
strList.add("谛听");
strList.add("abc732147");
strList.add("貔貅");
strList.add("hd1abc22");
strList.add("347abc");
strList.add("鲲");
for (int j = 0; j < strList.size(); j++) {
//如果元素包含"abc"就会被删除
if (strList.get(j).contains("abc")) {
//但是这样写如果相邻元素都包含"abc"就删不掉了
strList.remove(j);
//加个 j-- 就可以了
j--;
}
}
System.out.println(strList);
//输出: [谛听, 貔貅, 鲲]
}
}
2.ListIterator接口 (List集合自己的迭代器)
public class ListIteratorDemo {
public static void main(String[] args) {
List<String> strList = new ArrayList<>();
strList.add("谛听");
strList.add("山新");
strList.add("哪吒");
//通过 for循环遍历
for (int i = 0; i < strList.size(); i++) {
System.out.print(strList.get(i)+"\t");
}
System.out.println();
System.out.println("==============================");
//创建一个List迭代器对象
ListIterator<String> stringListIterator = strList.listIterator();
//通过迭代器进行打印
//从前往后打印
while (stringListIterator.hasNext()){
String next=stringListIterator.next();
System.out.print(next+"\t");
}
System.out.println();
System.out.println("==============================");
//从后往前打印
while (stringListIterator.hasPrevious()){
String previous=stringListIterator.previous();
System.out.print(previous+"\t");
}
}
}
3.Stack类(List的一个间接子类)
也是一种集合
public class StackDemo {
public static void main(String[] args) {
//创建一个栈集合
Stack<String> stack = new Stack<>();
stack.push("1");
stack.push("2");
stack.push("3");
stack.push("4");
// peek() 得到 stack集合的最后一个元素,不会将其删除
System.out.println(stack.peek());
// pop() 也是得到最后一个元素,并将其删除
System.out.println(stack.pop());
System.out.println(stack);
}
}
4.Queue接口 (队列的意思)
也是一种集合
public class QueueDemo {
public static void main(String[] args) {
//创建一个 Queue队列,可以指定一个容量,也可以不指定
Queue<Integer> queue= new ArrayBlockingQueue<>(20);
// 1. add() 添加一个元素
queue.add(100);
queue.add(200);
queue.add(300);
queue.add(400);
queue.add(500);
queue.add(600);
// 2. offer() 也是添加一个元素,但可以返回一个布尔值,越界为 false
queue.offer(100);
// 3. element() 取第一个元素,但不会删除
System.out.println(queue.element());
// 4. poll() 取第一个元素,并将其删除,删完了就输出 null
System.out.println(queue.poll());
// 5. remove() 取第一个元素,将其删除,没有元素了就抛异常
System.out.println(queue.remove());
// Queue队列里面没有与索引相关的方法
}
}
5.PirorityQueue类
也是一种集合
不过它是按从小到大保存元素的,而不是按添加顺序保存的
public class PriorityQueueDemo {
public static void main(String[] args) {
//创建 PriorityQueue队列
//通过在创建的时候传入一个 Comparator接口对象,可以指定排序的标准
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
//这里原来写的是 o1-o2,是从小到大排序,
//改成 -(o1-o2) 就可以从大到小排序了
return -(o1 - o2);
}
});
// PriorityQueue队列不可以存储 null
pq.add(1);
pq.add(12);
pq.add(-1);
pq.add(1);
pq.add(31);
pq.add(10);
pq.add(19);
//直接输出的话元素是没有顺序的
System.out.println(pq);
//这样就可以从小到大输出了
while (pq.size() > 0) {
System.out.print(pq.poll() + "\t");
}
System.out.println();
}
}
6.ArrayDeque双端队列(了解)
就是从前添 从后添 从前取 从后取 从前删 从后删 都可以
有 addFirst() addLast() getFirst() getLast() removeFirst() removeLast() 等方法
7.LinkedList()链表(了解)
8.Map集合
一次存两个值,一个 key值 一个 value值
两个值互相对应
比如 一个学号 对应 一个学生 (1001,张三)
如果 key值 重复,则会把之前的元素覆盖掉
public class HarshMapDemo {
public static void main(String[] args) {
//这次要规定两个类型了
HashMap<String, Student> hashMap=new HashMap<>();
// 1. put() 添加元素
hashMap.put("1001",new Student("清凝",20));
hashMap.put("1002",new Student("老君",5894));
hashMap.put("1003",new Student("坛九",12));
hashMap.put("1004",new Student("哪吒",10));
// 2. containsKey() 检查集合中是否有传入的 kry值,有返回true
System.out.println(hashMap.containsKey("1001"));
// 3. containsKey() 检查集合中是否有传入的 value值,有返回true
System.out.println(hashMap.containsValue(new Student("坛九",12)));
// 4. remove() 拿到一个元素,并删除它
System.out.println(hashMap.remove("1001"));
System.out.println(hashMap);
}
}
๑Ő௰Ő๑) THE END