Java array

A brief introduction to arrays

  • An array is a combination of multiple data of the same type to achieve unified management of these data
  • An array is a reference, and the array data is an Object.
  • The elements in an array can be of any data type, including primitive and reference types
  • The array type is unitary and can only hold the same type of data
  • Once an array is created, the size is fixed
  • Each element in the array has its own position (subscript), starting at 0

So arrays are typically used to store data of a fixed size: for example, to store class grades

100,90,98,99,88,80 int scores = {}

Create an array

Key Array Keyword :[]

Int a[] is the same as int a[]. [] is the same as int a[].

2.1 Only declare no assignment

type [] var;或者 type var [] ;

Such as:

int score[]; int [] score; Object [] obj; //Object is the ancestor of Java syntax

2.2 Declare and assign values

type [] var = new type[size]; // The element of the array is empty. The default value is the default value of the type

type [] var = {elm,elem2,elem3,… } // The data is assigned at the same time it is created

Public static void main(String[] args) {int score[] = new int[9]; for(int i = 0; i<score.length; i++) { System.out.print(score[i]+"\t"); } System.out.println("------------"); Int score2[] = {100,90,88,85,66}; int score2[] = {100,90,88,85,66}; for(int i = 0; i<score2.length; i++) { System.out.print(score2[i]+"\t"); }

2.3 new array memory model

New: Java keyword used to create new objects in memory by opening up new space

2.4 Subscript operation elements

2.4.1 Assign values to elements by subscript

int score[] = new int[9]; // Score [2] = 10; score[5] = 50;

2.4.2 Obtain element content by subscript

int num2 = score[2]; System.out.println("num2="+num2); for(int i = 0; i<score.length; i++) { System.out.print(score[i]+"\t"); }

2.5 Precautions

  • Java uses the keyword new to create array objects
  • Each element in an array can be referenced only after it is allocated space with the operator new.
  • Array element reference: array name [array element subscript]

    • The index of an array element can be an integer constant or an integer expression. A [3], B [I], C [6* I];
    • Array elements start with index 0; The range of valid subscripts for arrays of length n is 0-n-1; Such as int a [] = new int [3]. A [0], A [1], A [2]
  • Each array has a length attribute that specifies its length. For example, a.length specifies the length of array a (number of elements).

Three, traverse the number set

The traversal array can be used to traverse elements by subscripting, or by enhancing the foreach loop

3.1 Normal for loop

Since we can access elements using arr[index], we can loop through arr.length to retrieve the values of different indexes.

Note that the index range is 0-(length-1), and cannot be greater than or equal to length because subscripts are evaluated from 0. Otherwise, an array out of bounds exception is thrown

100,90,88,85,66 int score [] = {}; for(int i = 0; i<score.length; i++) { System.out.println(score2[i]); }

All elements in the score array are iterated once and printed out

3.2 Enhance the for loop

Java has a powerful looping structure that allows you to process each element of an array in turn without the distraction of specifying a subscript value. The statement format of this enhanced for loop is:

for(variable : arr)statement

Such as:

100,90,88,85,66 int score [] = {}; for(int ele:score){ System.out.println(ele); }

This syntax is the same as the normal for loop above. This for each loop is much cleaner and less error-prone

Four, array copy

If we want to copy all the values of an array into a new array, we use the CopyOf method of the Arrays class:

The second parameter is the length of the new array. This method is usually used to increase the size of an array

int score[] = {100, 90, 88, 85, 66}; Int [] newArr01 = copyOf(score, 10); Int [] neWarR02 = copyOf(score, 3); System.out.println(" Arrays = "+ Arrays.toString(score)); System.out.println(" new Arrays = "+ Arrays.toString(newArrays.toString (arrays.toString)); System.out.println(" new Arrays = "+ Arrays.toString(newArrays.toString (arrays.toString));

Note:

  • If the array elements are numeric, the extra elements will be assigned a value of 0.
  • If the array element is of Boolean type, the value is assigned to false.
  • Conversely, if the length is less than the length of the original array, only the first data element is copied.

Five, array sorting

5.1 Sorting of the Arrays utility class

Java.util. Arrays: belong to utility classes, so you must parent packages before you can use them

Public static void main(String[] args) {int score [] = {99,89,78,89,70}; String ch[] = {"B","C","A","F","D"}; System.out.println(" before sort :"); for (int i = 0; i < ch.length; i++) { System.out.print(ch[i]+"\t"); } System.out.println(); 1. Sort in ascending order from small to large without descending order. 2. Also called the natural order. A(65) B(66) C(67) D(68) Arrays.sort(ch); System.out.println(" after sorting :"); for (int i = 0; i < ch.length; i++) { System.out.print(ch[i]+"\t"); }}

4.2 Sorting by Algorithm (Bubble)

For example, in ascending order:

  1. You take two adjacent numbers, you compare them, and you move the larger number back. Loop through the comparison to find the maximum number
  2. Since only one piece of data can be determined per loop, multiple nested loops are needed to complete the final sorting
  3. The details are as follows:

Public static void main(String[] args) {int score [] = {99,89,78,89,70}; / / I = 0 {89,78,88,69,70,99}; 78,88,69,70,89,99 / / I = {1}; 78,69,70,88,89,99 / / I = {2}; System.out.println(" the original result :"); for (int j = 0; j < score.length; j++) { System.out.print(score[j]+"\t"); } System.out.println(); // Sort: After the first few rows are sorted, the last one automatically determines the position. // select the largest for (int I = 0; i < score.length-1; I++) {// for (int j = 0; j < score.length-1-i; J ++) {if(score[j]>score[j+1]) {int temp = score[j]; score[j] = score[j+1]; score[j+1] = temp; }} System.out.println(" string "+(I +1)+" result :"); for (int j = 0; j < score.length; j++) { System.out.print(score[j]+"\t"); } System.out.println(); }}