赞
踩
在Java编程中,了解和掌握常见的数据结构是至关重要的。以下是几种常见的数据结构及其简单的示例代码:
- int[] numbers = new int[5]; // 创建一个长度为5的整型数组
- numbers[0] = 1; // 设置第一个元素的值为1
- int element = numbers[2]; // 获取第三个元素的值
- System.out.println(element); // 输出结果:0(默认值,因为没有赋值)
- class ListNode {
- int value;
- ListNode next;
- ListNode(int value) {
- this.value = value;
- this.next = null;
- }
- }
-
- // 创建链表
- ListNode head = new ListNode(1);
- ListNode node2 = new ListNode(2);
- ListNode node3 = new ListNode(3);
-
- head.next = node2;
- node2.next = node3;
-
- // 遍历链表
- ListNode current = head;
- while (current != null) {
- System.out.println(current.value);
- current = current.next;
- }
- import java.util.Stack;
-
- Stack<Integer> stack = new Stack<>();
- stack.push(1); // 添加元素到栈顶
- stack.push(2);
- stack.push(3);
-
- int topElement = stack.peek(); // 获取栈顶元素(不移除)
- System.out.println(topElement); // 输出结果:3
-
- int poppedElement = stack.pop(); // 弹出栈顶元素
- System.out.println(poppedElement); // 输出结果:3
- import java.util.LinkedList;
- import java.util.Queue;
-
- Queue<Integer> queue = new LinkedList<>();
- queue.offer(1); // 添加元素到队尾
- queue.offer(2);
- queue.offer(3);
-
- int frontElement = queue.peek(); // 获取队头元素(不移除)
- System.out.println(frontElement); // 输出结果:1
-
- int dequeuedElement = queue.poll(); // 出队队头元素
- System.out.println(dequeuedElement); // 输出结果:1
- import java.util.HashMap;
-
- HashMap<String, Integer> hashMap = new HashMap<>();
- hashMap.put("apple", 1);
- hashMap.put("banana", 2);
- hashMap.put("orange", 3);
-
- int value = hashMap.get("banana"); // 通过键获取值
- System.out.println(value); // 输出结果:2
-
- boolean containsKey = hashMap.containsKey("apple"); // 检查是否包含某个键
- System.out.println(containsKey); // 输出结果:true
-
- hashMap.remove("orange"); // 移除指定键的键值对
-
- for (String key : hashMap.keySet()) {
- int val = hashMap.get(key);
- System.out.println(key + ": " + val);
- }
HashSet
和TreeSet
。- import java.util.HashSet;
-
- HashSet<Integer> set = new HashSet<>();
- set.add(1);
- set.add(2);
- set.add(3);
-
- boolean contains = set.contains(2); // 检查是否包含某个元素
- System.out.println(contains); // 输出结果:true
-
- set.remove(3); // 移除指定元素
-
- for (Integer num : set) {
- System.out.println(num);
- }
- class TreeNode {
- int val;
- TreeNode left;
- TreeNode right;
-
- public TreeNode(int val) {
- this.val = val;
- this.left = null;
- this.right = null;
- }
- }
-
- class BinarySearchTree {
- private TreeNode root;
-
- public void insert(int val) {
- root = insertNode(root, val);
- }
-
- private TreeNode insertNode(TreeNode node, int val) {
- if (node == null) {
- return new TreeNode(val);
- }
- if (val < node.val) {
- node.left = insertNode(node.left, val);
- } else if (val > node.val) {
- node.right = insertNode(node.right, val);
- }
- return node;
- }
-
- public boolean search(int val) {
- return searchNode(root, val);
- }
-
- private boolean searchNode(TreeNode node, int val) {
- if (node == null) {
- return false;
- }
- if (val == node.val) {
- return true;
- } else if (val < node.val) {
- return searchNode(node.left, val);
- } else {
- return searchNode(node.right, val);
- }
- }
- }
-
- BinarySearchTree bst = new BinarySearchTree();
- bst.insert(5);
- bst.insert(2);
- bst.insert(8);
- bst.insert(1);
-
- System.out.println(bst.search(2)); // 输出结果:true
- System.out.println(bst.search(10)); // 输出结果:false
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Queue;
-
- class Graph {
- private int vertices; // 顶点数
- private List<List<Integer>> adjacencyList; // 邻接表表示图
-
- public Graph(int vertices) {
- this.vertices = vertices;
- adjacencyList = new ArrayList<>(vertices);
- for (int i = 0; i < vertices; i++) {
- adjacencyList.add(new ArrayList<>());
- }
- }
-
- public void addEdge(int source, int destination) {
- adjacencyList.get(source).add(destination);
- adjacencyList.get(destination).add(source);
- }
-
- public void breadthFirstSearch(int startVertex) {
- boolean[] visited = new boolean[vertices];
- Queue<Integer> queue = new LinkedList<>();
-
- visited[startVertex] = true;
- queue.offer(startVertex);
-
- while (!queue.isEmpty()) {
- int currentVertex = queue.poll();
- System.out.print(currentVertex + " ");
-
- List<Integer> neighbors = adjacencyList.get(currentVertex);
- for (int neighbor : neighbors) {
- if (!visited[neighbor]) {
- visited[neighbor] = true;
- queue.offer(neighbor);
- }
- }
- }
- }
- }
-
- Graph graph = new Graph(6);
- graph.addEdge(0, 1);
- graph.addEdge(0, 2);
- graph.addEdge(1, 3);
- graph.addEdge(2, 4);
- graph.addEdge(3, 5);
-
- graph.breadthFirstSearch(0); // 从顶点0开始广度优先搜索
- import java.util.PriorityQueue;
-
- PriorityQueue<Integer> minHeap = new PriorityQueue<>(); // 创建一个最小堆
- minHeap.offer(5); // 添加元素
- minHeap.offer(2);
- minHeap.offer(8);
- minHeap.offer(1);
-
- int minElement = minHeap.peek(); // 获取堆顶元素(最小值)
- System.out.println(minElement); // 输出结果:1
-
- while (!minHeap.isEmpty()) {
- int currentElement = minHeap.poll(); // 弹出并移除堆顶元素
- System.out.println(currentElement);
- }
ArrayList
和LinkedList
。- import java.util.ArrayList;
-
- ArrayList<Integer> myList = new ArrayList<>();
- myList.add(1); // 添加元素
- myList.add(2);
- myList.add(3);
-
- System.out.println(myList); // 输出结果:[1, 2, 3]
-
- myList.remove(1); // 移除指定索引位置的元素
- System.out.println(myList); // 输出结果:[1, 3]
-
- int element = myList.get(0); // 获取指定索引位置的元素
- System.out.println(element); // 输出结果:1
-
- for (int i = 0; i < myList.size(); i++) { // 遍历列表中的元素
- System.out.println(myList.get(i));
- }
这些示例代码展示了如何使用Java中的一些常见数据结构。你可以根据具体需求进行修改和扩展,以适应不同的应用场景。了解和熟练使用这些数据结构,将帮助你编写出更高效、可维护的代码。
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。