The data structure and algorithm in the Java core framework

The data structure and algorithm in the Java core framework Overview: Data structure and algorithm are the core concepts of computer science, and they are essential for software development.In the Java core framework, a wealth of data structure and algorithm are provided to help developers solve various problems more efficiently.This article will introduce the data structure and algorithm commonly used in the Java core framework, and provide relevant code examples. 1. Data structure: 1. Array: The array is one of the most basic data structures and is used to store a set of elements with the same data type.In Java, the size of the array is fixed, and the size needs to be specified at the time of creation.The following is an example of creating and accessing arrays: int [] number = new int [5]; // Create a integer array of 5 numbers [0] = 10; // Set the first element to 10 numbers [1] = 20; // Set the second element to 20 System.out.println (numbers [0]); // Output the value of the first element (10) System.out.println (numbers [1]); // Output the value of the second element (20) 2. LinkedList: Links are a commonly used dynamic data structure that stored in the linear sequential order and can increase or decrease elements at any time.The LinkedList class in Java implements a two -way linked list, which can be inserted and deleted at the beginning and end of the linked list.Below is an example of creating and operating linked lists: LinkedList <strong> names = new linkedlist <string> (); // Create a linked list names.add ("Alice"); // Insert elements at the end names.addfirst ("Bob"); // Insert elements at the beginning System.out.println (names); // The content of the output linked list ([Bob, Alice]) names.removelast (); // Remove the last element System.out.println (names); // The content of the output linked list ([BOB]) 3. Stack: The stack is a data structure that is followed by Lifo, similar to a box, and can only be put into and out of the element from the top.The Stack class in Java realizes the function of the stack.The following is an example of a stack: Stack <integer> stack = New Stack <Integer> (); // Create a stack stack.push (10); // Press element 10 into the stack stack.push (20); // Press element 20 into the stack System.out.println (stack.peek ()); // Output stack top element (20) stack.pop (); // pop the top element of the stack System.out.println (stack.peek ()); // Output stack top element (10) 4. Queue: The queue is an advanced first -out (FIFO) data structure, similar to the concept of waiting in line.There are multiple implementation classes (such as LinkedList and ArrayDeque) in the Queue interface in Java, which provides different queue implementation.The following is an example of using a queue: Queue <string> queue = new LinkedList <string> (); // Create a queue queue.offer ("Alice"); // Add the element alice to the end of the team queue.offer ("Bob"); // Add the element BOB to the end of the team System.out.println (queue.peek ()); // The output team's first element (alice) queue.poll (); // Remove the team's first element System.out.println (queue.peek ()); // The output team's first element (BOB) Second, algorithm: 1. Sorting algorithm: Sorting algorithm is used to arrange a set of elements in accordance with certain rules.Java provides a variety of sorting algorithms, such as bubbling sorting, selecting sorting, inserting sorting, and fast sorting.The following is an example of sorting the array using the fast sort algorithm: import java.util.Arrays; public class QuickSortExample { public static void main(String[] args) { int[] array = {5, 8, 2, 4, 1}; quickSort(array, 0, array.length - 1); System.out.println (arrays.tostring (array)); // output sorted array } public static void quickSort(int[] array, int low, int high) { if (low < high) { int pivotIndex = partition(array, low, high); quickSort(array, low, pivotIndex - 1); quickSort(array, pivotIndex + 1, high); } } public static int partition(int[] array, int low, int high) { int pivot = array[high]; int i = low - 1; for (int j = low; j < high; j++) { if (array[j] < pivot) { i++; swap(array, i, j); } } swap(array, i + 1, high); return i + 1; } public static void swap(int[] array, int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } } 2. Find algorithm: The search algorithm is used to find the specified element in a set of data.Java provides a variety of finding algorithms, such as sequential search, two -point search, and hash search.The following is an example of finding the specified element in an orderly array using a two -point search algorithm: public class BinarySearchExample { public static void main(String[] args) { int[] array = {2, 4, 6, 8, 10}; int target = 6; int result = binarySearch(array, target); if (result == -1) { System.out.println ("Element does not exist"); } else { System.out.println ("Element in the index" + result + "); } } public static int binarySearch(int[] array, int target) { int low = 0; int high = array.length - 1; while (low <= high) { int mid = (low + high) / 2; if (array[mid] == target) { return mid; } else if (array[mid] < target) { low = mid + 1; } else { high = mid - 1; } } return -1; } } Summarize: Data structure and algorithm are important content in the core framework of the Java. Proficient in mastering them is essential to develop efficient and optimized code.This article introduces the data structure and algorithm commonly used in the Java core framework, and provides relevant code examples.Developers can choose the appropriate data structure and algorithm according to actual needs to solve the problem.