1. What is an array
Arrays are collections of elements of the same data type, and array elements are arranged in a linear order.
When each element of an array is represented, the array's position is represented by the subscript of the arrays, where the subscript 0 is the first element of the array. The element subscript of the last array is the array length minus one.
The syntax for declaring an array is: data type [] Array name = new data type [size]; for example:
New STRING[10];
After the array is defined, the array has a default initialization value, and when we do not indicate the initialization value for it, the int default initialization value is 0;string default initialization value is null,double default initialization value is 0.0, etc.
One of the two-dimensional arrays is that the array elements are one-dimensional elements of a one-dimensional array. Cases:
int[][] in = int[3][2];//can be understood as this is a one-dimensional element with three elements, where each element value is a one-dimensional element
2. Array Access
By using an array, we can get the length of the array and the element values for each position of the array.
For example:
New String[10]; int len = str.length (); // get the length of the array System.out.println (Str[7]); // gets the element value labeled 7 in the array, which is the eighth element of the array
3. Classic extensions of arrays
1.99 Multiplication Table
classmultiplicationtable { Public Static voidMain (string[] args) {//I variables are used to control the number of rows for(inti = 1; I < 10; i++) { //J variables are used to control the maximum number of participating calculations in each row: equal to the number of rows for(intj = 1; J <= I; J + +) { //format the output and use "\ T" to control the layout alignmentSystem.out.print (j + "*" + i + "=" + J*i + "\ T")); } //after each line is finished, a newline is requiredSystem.out.println (); } } }
This is the representation of a common two-dimensional array, the outer loop controls the number of rows, and the inner loop control column count.
2. Bubble sort
Bubble sort, is a simpler sort algorithm. In the bubble sort algorithm, you need to repeat the sequence of visits to sort, compare two elements at a time, and swap them out if they are in the wrong size order. Since the decimal is always placed forward in the sorting process, the large number is placed backwards, which is the equivalent of bubbles rising, so called bubble sort.
classBubblesort { Public Static voidMain (string[] args) {//Create an array int[] arr =New int[10]; Random ran=NewRandom (); for(inti = 0; i < arr.length; i++) {Arr[i]= Ran.nextint (100); } System.out.println (Arrays.tostring (arr)); //Bubble SortSystem.out.println ("----------bubble sort Start----------"); for(inti = 0; i < arr.length-1; i++) { for(intj = 0; J < arr.length-i-1; J + +) { if(Arr[j] > arr[j + 1]) { intt =Arr[j]; ARR[J]= Arr[j + 1]; Arr[j+ 1] =T; }} System.out.println (Arrays.tostring (arr)); } System.out.println ("----------Bubble sort End----------"); System.out.println (arrays.tostring (arr)); }}
An array of basic Java learning