🔎 This is JAVA Chengxian road. Pay attention to my learning JAVA and don't get lost
👍 If it helps you, give the blogger a free praise to show encouragement
Welcome 🔎 give the thumbs-up 👍 Comment collection ⭐ ️
Array and two-dimensional array feel that it should be easier to understand by taking the equipment column of King glory as an example 😎. Starting from the foundation, the follow-up meeting will talk about JAVA advanced, and the middle will be interspersed with test questions and project practice. I hope it can bring you help!
What is an array
An array is an ordered sequence of elements. If you name a collection of a limited number of variables of the same type, this name is the array name. The variables that make up the array are called the components of the array, also known as the elements of the array, and sometimes also known as subscript variables / 12713827). The number used to distinguish the elements of an array is called a subscript. Array is a form in which several elements of the same type are organized in an orderly form in order to facilitate processing in programming. The collection of these ordered data elements of the same kind is called an array.
An array is a collection used to store multiple data of the same type.
Example - Equipment Grid
Array, element and subscript:
For example, when fighting the glory of the king, everyone should produce equipment, and everyone has his own equipment grid. Then the equipment column is an array, in which the equipment is the element, and the position of the equipment is the subscript.
In other words, each subscript corresponds to an equipment, and the subscript starts from 0, so the subscript corresponding to the first equipment is 0
Declaration array
int type
When declaring an array, the length of the array is fixed, and the length of the array is unchanged.
There are two declaration methods. The first one is direct assignment. In the second type of declaration, there is no assignment, but the length is fixed. Although there is no assignment, all elements will be assigned 0 by default.
public class Test { public static void main(String[] args) { //Declare an array of type int and initialize the assignment int[] a={1,2,3,4,5,}; //Declare the array, set the length of the value array, and initialize all to 0 int[] b=new int[10]; } }
String type
There is no difference between here and above. There are also two declaration methods.
public class Test { public static void main(String[] args) { //Declare an array of type int and initialize the assignment int[] a={1,2,3,4,5,}; //Declare the array, set the length of the value array, and initialize all to 0 int[] b=new int[10]; //Declare an array of String type and initialize the assignment String[] d={"aa","bb","cc"}; //Declare that the array has a fixed length. The default initialization is all 0 String[] c=new String[10]; } }
Array operation
Traversal array
Traversal array: there are two methods, for loop and for in loop
for loop, here I put three elements in the array, that is, equipment. Cyclic output, starting from the subscript 0
zb.length is the size of the array
public class Test { public static void main(String[] args) { String[] zb={"Cool boots","Bloody blade","Famous swordsman"}; for (int i = 0; i < zb.length; i++) { System.out.println(zb[i]); } } }
result:
for in loop, if you forget how to use this loop, you can refer to the previous article, which is described in detail in the golden elixir.
public class Test { public static void main(String[] args) { String[] zb={"Cool boots","Bloody blade","Famous swordsman"}; for (String s : zb) { System.out.println(s); } } }
Two dimensional array
A two-dimensional array is essentially an array with an array as an array element, that is, "array of arrays", type specifier array name [constant expression] [constant expression]. A two-dimensional array is also called a matrix, and a matrix with equal rows and columns is called a square matrix. Symmetric matrix a[i][j] = a[j][i], diagonal matrix: there are zero elements outside the main diagonal of the n-order square matrix.
A two-dimensional array is an ordinary one-dimensional array, in which each element is a one-dimensional array, and when combined, it is a two-dimensional array.
Continue with the previous example. At the beginning of each game, one party's data panel has a default sorting (the panel showing equipment and economy). Everyone has an equipment grid, which is equivalent to an array. Then there are five equipment columns (one team) on the information panel, which are arranged in the default order, which is also equivalent to an array. An equipment grid is an element, and the position of the equipment grid is the subscript. But each element in this array is also an array, so the data panel is equivalent to a two-dimensional array.
Declare a two-dimensional array
The method of declaring a two-dimensional array is no different from that of declaring an array
public class Test { public static void main(String[] args) { //Declare a two-dimensional array and assign a value int[][] a={{123},{456},{789}}; //Declare a fixed size for a two-dimensional array int[][] ns = new int[3][5]; } }
The above two-dimensional array is introduced with the glory of the king. Some readers may be a little confused. Now type out the above example with code.
I can't remember the name of the equipment here. I copied the equipment of the last three people directly
public class Test { public static void main(String[] args) { //Five people, each with three equipment in the equipment grid. String[][] wzry=new String[5][3]; //To buy equipment for the first person is to assign a value to the first array wzry[0][0]="gemstone"; wzry[0][1]="Blood knife"; wzry[0][2]="Buddha's gilded image"; //To buy equipment for the second person is to assign a value to the second array wzry[1][0]="Iron sword"; wzry[1][1]="Straw sandals"; wzry[1][2]="Armor"; //To buy equipment for a third person is to assign a value to the third array wzry[2][0]="gemstone"; wzry[2][1]="Blood knife"; wzry[2][2]="Buddha's gilded image"; //To buy equipment for the fourth person is to assign a value to the fourth array wzry[3][0]="gemstone"; wzry[3][1]="Blood knife"; wzry[3][2]="Buddha's gilded image"; //To buy equipment for the fifth person is to assign a value to the fifth array wzry[4][0]="gemstone"; wzry[4][1]="Blood knife"; wzry[4][2]="Buddha's gilded image"; } }
Now let's run it and see what everyone's equipment has
//The second equipment of the third person System.out.println("The second equipment of the third person"); System.out.println(wzry[2][1]); //The third equipment of the first person System.out.println("The third equipment of the first person"); System.out.println(wzry[0][2]); //All the equipment of the fifth person System.out.println("All the equipment of the fifth person"); for (int i = 0; i < 3; i++) { System.out.println(wzry[4][i]); }
result: