数组
创建数组
int[] a = {1, 2, 3};
System.out.println("a: "+Arrays.toString(a));
int[] b = new int[]{1, 2, 3};
int[] c = new int[3];
for(int i = 0; i < a.length; i++) {
c[i] = i+1;
}
ArrayList<Integer> arr = new ArrayList<>();
for(int i = 0; i < 3; i++) {
arr.add(i+1);
}
数组创建时长度即固定,ArrayList创建的长度不固定
添加元素
arr.add(99);
// [1,2,3,99]
arr.add(3, 88);
// [1,2,3,88,99]
访问元素
int c1 = c[1];
int arr1 = arr.get(1);
更新元素
c[1] = 11;
arr.set(1, 11);
删除元素
arr.remove(3);
数组长度
int cSize = c.length;
int arrSize = arr.size();
遍历数组
for (int i = 0; i < c.length; i++) {
int current = c[i];
System.out.println("c at index "+i+": "+current);
}
for (int i = 0; i < arr.size(); i++) {
int current = arr.get(i);
System.out.println("c at index "+i+": "+current);
}
查找元素
for (int i = 0; i < c.length; i++) {
if (c[i] == 99) {
System.out.println("we found 99 in c!");
}
}
boolean is99 = arr.contains(99);
数组排序
c = new int[]{2,3,1};
Arrays.sort(c);
arr = new ArrayList<>();
arr.add(2);
arr.add(3);
arr.add(1);
Collections.sort(arr);
Collections.sort(arr, Collections.reverseOrder());
链表
创建链表
LinkedList<Integer> list = new LinkedList<>();
添加元素
list.add(1);
list.add(2);
list.add(3);
System.out.println(list.toSring());
list.add(2, 99);
访问元素
int element = list.get(2);
// 99
搜索元素
int index = list.indexOf(99);
// 2
更新元素
list.set(2, 88);
// [1,2,88,3]
删除元素
list.remove(2);
// [1,2,3]
长度
int length = list.size();
队列
创建队列
Queue<Integer> queue = new LinkedList<>();
添加元素
queue.add(1);
queue.add(2);
queue.add(3);
获取即将出队的元素
int temp1 = queue.peek();
删除即将出队的元素
int temp2 = queue.poll();
判断队列是否为空
System.out.println(queue.isEmpty());
长度
int length = queue.size();
遍历队列
while (!queue.isEmpty()) {
int temp = queue.poll();
System.out.println(temp);
}
栈
创建栈
Stack<Integer> stack = new Stack<>();
添加元素
stack.push(1);
stack.push(2);
stack.push(3);
获取栈顶元素
stack.peek();
删除栈顶元素
int temp = stack.pop();
栈的大小
stack.size();
栈是否为空
stack.isEmpty();
栈的遍历
while (!stack.isEmpty()) {
int num = stack.pop();
System.out.println(num);
}
哈希表
创建哈希表
String[] hashTable = new String[4];
HashMap<Integer, String> map = new HashMap<>();
添加元素
hashTable[1] = "hanmeimei";
hashTable[2] = "lihua"
hashTabel[3] = "siyangyuan"
map.put(1, "hanmeimei")
map.put(2, "lihua")
map.put(3, "siyangyuan")
更新元素
hashTable[1] = "bishi";
map.put(1, "bishi");
删除元素
hashTable[1] = "";
map.remove(1);
获取元素
String temp = hashTable[3];
map.get(3);
检测key存在
map.containsKey(3);
长度
map.size();
map.isEmpty();
集合
创建集合
HashSet<Integer> set = new HashSet<>();
添加元素
set.add(10);
set.add(3);
set.add(5);
set.add(2);
set.add(2);
打印顺序不确定
搜索元素
set.contains(2);
删除元素
set.remove(2);
长度
set.size();
堆
import java.util.Collections;
import java.util.PriorityQueue;
public class Test {
public static void main(String[] args) {
// 最小堆
PriorityQueue<Integer> minheap = new PriorityQueue<>();
// 最大堆
PriorityQueue<Integer> maxheap = new PriorityQueue<>(Collections.reverseOrder());
// 添加元素
minheap.add(10);
minheap.add(8);
minheap.add(9);
minheap.add(11);
minheap.add(2);
// [2, 8, 9, 11, 10]
maxheap.add(10);
maxheap.add(8);
maxheap.add(9);
maxheap.add(11);
maxheap.add(2);
// [11, 10, 9, 8, 2]
// 堆顶元素
minheap.peek();
maxheap.peek();
// 删除堆顶元素
minheap.poll();
maxheap.poll();
// 长度
minheap.size();
maxheap.size();
// 遍历
while (!minheap.isEmpty()) {
System.out.println(minheap.poll());
}
}
}