This article provides a brief description of arrays and basic usage of concepts and arrays.
I. Basic concepts of arrays
1.1. The Status of Arrays
Arrays are one of the most important data structures in any programming language. At the same time, different languages have different implementations and processing methods. But programmers know the practicability of arrays and the value of arrays and the importance of arrays.
1.2. What is an array?
An array is an ordered set of data of the same type.
(2) The use of arrays, which describe a number of data of the same type, is arranged and combined in a certain order. Each data is called an array element, and each array element can be accessed by a subscript.
1.3. Characteristics of arrays
(1) The length of the array is determined. Once the array is created, the future length of the array can not be changed, and the array is immutable.
(2) The element types of arrays are the same!
(3) Arrays are ordered. Each element of an array can be accessed by a subscript, which starts at 0.
Arrays are reference types
1.4. Storage of arrays on the stack
First, a variable score is declared in the stack, and the array refers to the data type, so score only declares that a variable is not assigned in the stack.
2, new int []; Represents applying for a space in the heap area, generating a memory address and assigning the memory address to score
Third, scroe finds the space of the corresponding address in the heap by the memory address
2. The Use and Practical Application of Arrays
2.1. Declaring Array Method
Data Type [] Array Name = new Array Type {Content} }
(2) Array type [array name = new [array length];
(3) Array Type [Array Name = new Array Type] {Content... }
2.2. Implementing sorting of arrays
The Principle and Operation Mode of Bubble Sorting Method
The principle of the bubble sorting algorithm is as follows:
Compare adjacent elements. If the first one is bigger than the second one, exchange the two.
Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. At this point, the last element should be the largest number.
Repeat the above steps for all elements except the last one.
Continue repeating the above steps for fewer and fewer elements each time until no pair of numbers need to be compared.
**public class Test02 { public static void main(String[] args) { int [] a = {2,4,8,9,6,7,5,1,3}; int tmp; for(int i=0;i<a.length;i++) { for (int j = 0; j < a.length-1; j++) { if (a[j]>a[j+1]) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } System.out.print(a[i]+"\t"); } System.out.println(); for(int i = 0;i<a.length;i++ ) { System.out.print(a[i]+"\t"); } } }**
(2) How to input 10 characters in console for sorting and output in reverse order (dichotomy)
import java.util.Scanner; import javax.sql.rowset.FilteredRowSet; public class Test01 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); char[] a = new char[10]; //Declarations array String str; for (int i = 0; i < a.length; i++) { System.out.println("Please enter _____________" + i + "A character"); str = sc.next(); a[i] = str.charAt(0); } for (int i = 0; i < a.length; i++) { System.out.print(a[i] + "\t"); } char tmp; for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1 - i; j++) { if (a[j] > a[j + 1]) { tmp = a[j]; a[j] = a[j + 1]; a[j + 1] = tmp; } } } System.out.println(); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + "\t"); } System.out.print("Using Dichotomy to Realize Inverse Output"); tmp = '\u0000'; for (int i = 0; i < a.length / 2; i++) { tmp = a[i]; a[i] = a[a.length - 1 - i]; a[a.length - 1 - i] = tmp; } System.out.println(); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + "\t"); } } }
III. Array classes
3.1. In addition to the above methods, jdk also provides java.util.Array classes that contain common array operations, providing convenience for daily development. The Arraysl class contains common operations for arrays (arranging, finding, filling, printing content, etc.).
(1) Array class is used to sort array elements
import java.util.Arrays; import java.util.Scanner; import javax.sql.rowset.FilteredRowSet; public class Test01 { private static Scanner sc; public static void main(String[] args) { int[] a = { 1, 2, 9, 7, 5, 6, 4, 8, 3 }; System.out.println(Arrays.toString(a)); Arrays.sort(a); System.out.println(Arrays.toString(a)); } }
(2) Using Array class to realize binary search
public class Test01 { public static void main(String[] args) { int [] a ={1,2,9,7,5,6,4,8,3}; System.out.println(Arrays.toString(a)); //To find by dichotomy, arrays must be sorted first Arrays.sort(a); System.out.println(Arrays.toString(a)); //Returns the new index position after sorting, if the negative number is not found System.out.println("Index of this element:"+Arrays.binarySearch(a,8)); } }