Java Basic Tutorial-Array

Java Basic Tutorial-Array

3. the array

Overview of arrays

One-dimensional array

Knowledge points of one-dimensional array

Initialization of the array

Index and length of the array

Traverse the array

The default value of the array (declared that there is no value assigned, print the default value of the array)

Memory diagram:

Memory analysis:

Multidimensional Arrays

Two-dimensional array knowledge points

Declaration and initialization of two-dimensional arrays

Index and length of two-dimensional array

Two-dimensional array traverse two levels of for loop

The initial value of the two-dimensional array

Two-dimensional array memory analysis:

Common algorithms in arrays

data structure

  • Yanghui Triangle

public static void main (String[] args) { int [][] yangHui = new int [ 10 ][]; for ( int i= 0 ;i<yangHui.length;i++){ yangHui[i] = new int [i+ 1 ]; } yangHui[ 0 ][ 0 ]= 1 ; for ( int i= 1 ;i<yangHui.length;i++){ yangHui[i][ 0 ] = 1 ; yangHui[i][i]= 1 ; for ( int j = 1 ;j<i;j++){ yangHui[i][j]=yangHui[i- 1 ][j- 1 ]+yangHui[i- 1 ][j]; } } System.out.println( " -Print Yanghui triangle-" ); for ( int i= 0 ;i<yangHui.length;i++){ for ( int j= 0 ;j<yangHui[i].length;j++){ System.out.print(yangHui[i][j]+ "\t" ); } System.out.println(); } } Copy code
  • Find the sum, maximum, minimum, and average of an array

public static void main (String[] args) { //Random array //define array, sum, min, max, avg int [] arr = new int [ 10 ]; int sum = 0 ; int min,max; float avg; //Attach a random value to the array for ( int i = 0 ;i<arr.length;i++) { //First assign a value of 1-100 to the element arr[i] = ( int ) ((Math.random() * 90 ) + 10 ); //Find the sum sum +=arr[i]; } //Find the average avg = sum/10f ; //Find the maximum and minimum min = arr[ 0 ]; max = arr[ 0 ]; for ( int i= 0 ;i<arr.length;i++){ if (arr[i]>=max){ max = arr[i]; } if (arr[i]<=min){ min = arr[i]; } } //Output the result System.out.println( "Array:" +Arrays.toString(arr)); System.out.println( "Sum:" +sum); System.out.println( "Average:" +avg); System.out.println( "Maximum:" +max); System.out.println( "Minimum:" +min); } Copy code
  • Copy of array

public static void main (String[] args) { int [] arr1 = new int []{ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 }; System.out.println(Arrays.toString(arr1)); //It cannot be called a copy of an array, it is to assign the address value of arr1 to arr2 int [] arr2 = arr1; for ( int i= 0 ;i<arr2.length;i++){ if (i% 2 == 0 ) { arr2[i]=i; } } System.out.println(Arrays.toString(arr2)); System.out.println(Arrays.toString(arr1)); Copy code

int[] arr2 = arr1;
Not copying the array, but assigning the address value of arr1 to arr2

To copy an array, first create a new array

Copy the array:

public static void main (String[] args) { int [] arr1 = new int []{ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 }; System.out.println(Arrays.toString(arr1)); //Copy of array int [] arr3 = new int [arr1.length]; for ( int i = 0 ;i<arr1.length;i++){ arr3[i]=arr1[i]; } for ( int i = 0 ;i<arr3.length;i++){ if (i% 2 == 0 ){ arr3[i]=i; } } System.out.println(Arrays.toString(arr1)); System.out.println(Arrays.toString(arr3)); } Copy code

  • Inversion of array
public static void main (String[] args) { //The inversion of the array String[] nickname = new String[]{ "JJ" , "DD" , "MM" , "GG" , "SS" , "AA" }; //Define an intermediate number and perform data exchange String temp; for ( int i = 0 ;i<nickname.length/2 ;i++){ temp = nickname[i]; nickname[i]=nickname[nickname.length- 1 -i]; nickname[nickname.length- 1 -i]=temp; } System.out.println(Arrays.toString(nickname)); //Output: [AA, SS, GG, MM, DD, JJ] } Copy code
  • Array search (linear search, binary search)

Linear search: look down one by one

public static void main (String[] args) { //Search (or search) //Linear search: look down one by one String[] nickname = new String[]{ "JJ" , "DD" , "MM" , "GG" , "SS" , "AA " }; //Whether to find the identity of the same element boolean isFlag = true ; String desc = "DD" ; for ( int i= 0 ;i<nickname.length;i++){ if (desc.equals(nickname[i])){ System.out.println( "The same element was found, the subscript is " +i); isFlag = false ; break ; } } if (isFlag== true ){ System.out.println( "There are no identical elements" ); } Copy code

Binary search (premise: the array to be searched must be in order)

public static void main (String[] args) { //Binary search //Prerequisite: the array to be searched must be ordered int [] numbers = new int []{- 23 ,- 13 ,- 4 , 2 , 9 , 34 , 99 , 100 };//arrange from small to large int target = 99 ; //the first index value at the beginning int head = 0 ; //the end index value at the beginning int end = numbers.length- 1 ; boolean isFlag1 =true ; while (head<=end){ int mid = (head+end)/2 ; if (numbers[mid] == target){ System.out.println( "The same element was found, the subscript is: " +mid); isFlag1 = false ; break ; } else if (numbers[mid]>target){ end = mid- 1 ; } else { //numbers[mid]<target head = mid+ 1 ; } } if (isFlag1){ System.out.println( "There are no identical elements" ); } } Copy code
  • Sorting algorithm in array elements

Bubbling sorting (to be able to write code)

public static void main (String[] args) { //Bubble sort int [] numbers = new int []{- 23 , 0 ,- 4 , 2 ,- 9 , 134 , 99 , 100 }; //adjacent Comparing two numbers, the larger number moves backward for ( int i = 0 ;i<numbers.length- 1 ;i++){ for ( int j = 0 ;j<numbers.length- 1 -i;j++){ if(numbers[j]>numbers[j+ 1 ]){ int temp = numbers[j]; numbers[j] = numbers[j+ 1 ]; numbers[j+ 1 ] = temp; } } } System.out.println(Arrays.toString(numbers)); } Copy code

Quick sort (understand thinking)

Sorting algorithm performance comparison

Use of Arrays tool class

public static void main (String[] args) { //Compare whether two arrays are equal int [] arr1 = new int []{- 23 , 0 , -4 , 2 ,- 9 }; int [] arr2 = new int []{- 23 , 0 , -4 , 2 ,- 9 }; boolean isEquals = Arrays.equals(arr1, arr2); System.out.println(isEquals); //Output the information of the array System.out.println(Arrays.toString(arr1)); //Fill the specified value into the array: replace each element with value Arrays.fill(arr1, 15 ); System.out.println(Arrays.toString(arr1)); //sort Arrays.sort(arr2); System.out.println(Arrays.toString(arr2)); //Binary search, the premise is ordered int [] arr3 = new int []{- 23 ,- 13 ,- 4 , 2 , 9 , 34 , 99 , 100 }; int index = Arrays.binarySearch(arr3, 32 ) ; //Negative number means there is no such number System.out.println(index); } Copy code

Common exceptions in the use of arrays

Null pointer: the address value is null and does not point to the heap