Introduction to JAVA array and Arrays

JAVA array learning

Quote: when storing the same type of data, if storing one by one will lead to too many variable names, consider using an array variable to store them and accessing each member in the array through subscript

One dimensional array

In order to generate an available array, we not only declare the array (give the attribute of name and member type), but also allocate memory space for it, and then conduct automatic initialization or manual initialization

Declaration array

The method is as follows:

Data type array name [] or data type [] array name

Allocate space

The method is as follows:

Array variable name = new data type [array length]

You can also allocate space while defining the array, for example:

int temp[] = new int[10];

Note: once the array is generated, it is not allowed to increase its space

Initialization of array

Automatic initialization:

Initial value of numerical type: 0;

Initial value of character type: '\ 0';

Initial value of boolean type: false;

Initial value of class object: null;

You can also assign values manually by subscripts one by one, or initialize the array directly after declaring it. At this time, the system will automatically determine the length of the array and automatically allocate space to it. The format is as follows:

Data type array name [] = {value 1, value 2,..., value n}

Note: Global initialization is allowed, but global assignment is not allowed, such as:

char s[ ] = new char[5];

s[ ] = {'a','b','c','d','e'} ;

Access array

Access by subscript as follows:

Array name [subscript]

Two dimensional array

How to understand multidimensional arrays

Starting from the two-dimensional array, for example:

int temp [] [] = new int [2] [3];

It can be understood that an array with two rows and three columns is generated, but a better understanding and common way is the form of array set array. Just like this example, sir, an array with a length of 2 is generated in each array, and then an array with a length of 3 is generated in each array, just like a forked branch

Common methods of array

How to get array length

Array name length

How to traverse an array more simply

Use the for... Statement, for example:

//Array initialized or assigned

for(int e : a){
sum = sum+e;
}

The colon ":" in the program means in. For (int e: a) is for each int e in a, that is, "for each integer E in array a". Each element in the array is represented by defining an integer variable E.

Print an array

In addition to using system out. In addition to println() and other similar methods, there is also display (array name), which is used to show the full picture of the array, separated by spaces, and wrap when the output is finished

Arrays class

Arrays class and basic usage

In order to operate Arrays more conveniently, java is in the package java Util defines a class of Arrays, which contains several static methods decorated with static

  • Use import Java util. Arrays; Import this class;
  • Use Arrays The method name (array) operates on an array.

Common methods of Arrays class

Method nameusage
toString methodThis method mainly returns the string form of array elements
equals methodCompare whether the contents of two arrays are equal
fill methodThis method is mainly used to replace the elements in the array
sort methodThis method is mainly used to sort array elements
copyOf methodThis method is mainly used to copy arrays
copyOfRange methodThis method is mainly used to copy the array, and the starting position needs to be specified
binarySearch methodThis method is mainly used to find the index value of the specified element
compare methodCompare two arrays in dictionary order

Detailed introduction of fill method

The fill method is used to replace elements in the array. It can either replace all elements in the array directly or replace some elements by specifying the index range.

Directly replace all elements in the array:

fill(arr, value)

Parameter Description:

  • The first parameter (arr) is an array;
  • The second parameter (value) is the replacement value.

Example: replace all elements in the array with 8.

 public static void main(String[] args) {        
 int [] arr={2,3,4,8,-2,0,-43,88};        
 Arrays.fill(arr,8);     // Use the fill method to replace all the values in the array with 8 system out. println(Arrays.toString(arr));     //  Print the values in the new array    
 }

Execution result:

[8, 8, 8, 8, 8, 8, 8, 8]

Replace some elements

fill(arr, fromIndex,toIndex, value)

Parameter Description:

  • The first parameter (arr) is an array;
  • The second parameter (fromIndex) is the starting index value (including) that needs to be replaced in the array;
  • The third parameter (toIndex) is the end index value (not included) that needs to be replaced in the array;
  • The fourth parameter (value) is the replacement value.

Example: replace some elements in the array with 8.

 public static void main(String[] args) {        
 // Please write code between begin and end        
 /********** Begin **********/        
 int [] arr={2,3,4,8,-2,0,-43,88};        
 Arrays.fill(arr,1,5,8);     // Use the fill method to replace some values in the array with 8 system out. println(Arrays.toString(arr));     //  Print the values in the new array / ************** End ***********************/}

Execution result:

[2, 8, 8, 8, 8, 0, -43, 88]

Detailed introduction to sort method

The sort method is used to sort the array elements. The default sorting method is ascending.

Example:

    public static void main(String[] args) {        
    int [] arr={8,2,7,84,12,1,786,92,34};     // Define the original array arrays sort(arr);     //  Sort array elements system out. print(Arrays.toString(arr));     //  Print array    
    }

Execution result:

[1, 2, 7, 8, 12, 34, 84, 92, 786]

The copyOf method and copyOfRange method are introduced in detail

Both the copyOf method and the copyOfRange method are used to copy arrays. The copyOfRange method needs to specify the starting position. Example:

public static void main(String[] args) {        
int [] arr={8,2,7,84,12,1,786,92,34};     // Define original array        
int[] ints = Arrays.copyOf(arr, 4);// Copy the array, and the second parameter specifies the length of the new array         
// Copy the array. When the length of the specified new array is greater than the original array, the default value will be filled in        
int[] ints1=Arrays.copyOf(arr,10);           System.out.println(Arrays.toString(ints));     // Print array system out. print(Arrays.toString(ints1));     //  Print array        
int[] ints2 = Arrays.copyOfRange(arr, 2, 4);     
// Copy the array, and the second and third parameters specify the index value        
// Copy the array. When the length of the specified new array is greater than the original array, the default value will be filled in        
int[] ints3 = Arrays.copyOfRange(arr, 0, 10);             System.out.println(Arrays.toString(ints2));     // Print array system out. print(Arrays.toString(ints3));     //  Print array    
}

Execution result:

[8, 2, 7, 84]
[8, 2, 7, 84, 12, 1, 786, 92, 34, 0]
[7, 84]
[8, 2, 7, 84, 12, 1, 786, 92, 34, 0]

binarySearch method

The binarySearch method is used to find the index value of the specified element. Because this method uses binary search, you must call the sort() method to sort before calling this method. If the array is not sorted, the result is uncertain.

Example:

 public static void main(String[] args) {        
 int [] arr={8,6,84,12,3,786,7,34};     // Define original array        
 int i = Arrays.binarySearch(arr, 7);     // The array is not sorted. Find the element system directly out. println(i);     //  Print index values       
 Arrays.sort(arr);     // Sort the original array system out. println(Arrays.toString(arr));     //  Print sorted array       
 int x = Arrays.binarySearch(arr, -2);     // After sorting, find the element that does not exist in the array. If it does not exist, it will output - 1       
 int y = Arrays.binarySearch(arr, 7);     // After sorting, find the element system. Existing in the array out. println(x);     //  Print index values       
 System.out.print(y);     // Print index values   
 }

Execution result:

-3[3, 6, 7, 8, 12, 34, 84, 786]-12

Use of compare method and equals method

The common use of the two methods is the same, compare (array 1, array 2) and equals (array 1, array 2). The former compares the elements in the array one by one according to the dictionary and returns the Boolean value, while the latter compares whether the contents in the two arrays are the same and returns the Boolean value. However, there is the method of "= =", At this time, the comparison is not only whether the contents of the two arrays are the same, but also whether the addresses of the two arrays are the same, and the Boolean value is returned accordingly

For detailed use of specific methods, please refer to the JDK 11 API java Chinese help document

Keywords: Java array

Added by funsutton on Sun, 30 Jan 2022 04:33:04 +0200