Array: is a collection of related variables
An array is a collection of related data. An array is actually a series of variables. Arrays can be divided into one-dimensional arrays, two-dimensional arrays, and multi-dimensional arrays according to their use.
The data is a bit
Define 100 shaping variables without using arrays: int i1; int i2; int i3
Use array to define int i[100];
Array definition: int i[100]; is just a pseudo-code, just expressing the meaning
One-dimensional array
One-dimensional arrays can store tens of millions of data, and the types of these data are exactly the same.
Using a java array, you must go through two steps, declare the array and allocate memory to the array.
Declaration Form 1
Declare one-dimensional array: data type array name[]=null;
Unallocated memory to array: array name = new data type [length];
Declaration Form 2
Declare a one-dimensional array: data type[] array name = null;
Java data types are divided into two categories
Basic data types
When int and long operation, it is the specific content itself
Reference data types: array, class, interface
The reference passes the right to use a memory, a piece of memory space, and multiple people may use it at the same time.
Case declaration array
package com.qn.array;public class Test { public static void main(String[] args) { int score[]=null;//Declare array score=new int[3];//Develop space, size 3 }}In the declaration format of an array, the data type is the data type of the array element, and the common ones are shaping, floating point type, and character type.
The array name is used to unify the names of the same set of elements of the same data type, and its naming rules are the same as the variables.
After the array is declared, the name of this array is actually saved in the stack memory. It is concluded that the memory required for the array is configured in the heap memory. The fixed production is to tell the compiler how many elements to store in the declared array, and new is to command the compiler to use the length in the brackets.
The default value of the basic data type is even read: int 0; as long as it is a reference data type, the default value is null
case
package com.qn.array;public class Test { public static void main(String[] args) { int score[]=null;//Declare array score=new int[3];//Develop space, size 3 System.out.print("score[0]="+score[0]); System.out.print("/tscore[1]="+score[1]); System.out.print("/tscore[2]="+score[2]); }}Interpretation of stack memory
In array operations, the name of the array is always saved in the stack memory. Only the space in the stack is opened. The array is never available. It must have a pointing pair of memory to be used. To open up a new pair of memory space, the new keyword must be used. Then the right to use the memory to the corresponding stack memory is handed over to the corresponding stack memory. Moreover, a heap memory space can be pointed to by multiple stack memory spaces at the same time. For example, a person can have multiple names, and the person is equivalent to the pair of memory, and the name is equivalent to the stack memory.
Allocate memory space while declaring an array
Declare array without memory
Data type array name[]=new Data type[number]
int score[]=new int[10];
Declare a shaping array score with 10 elements, and at the same time, create a memory space usage
In Java, since the shaping data type takes up 4 bytes, there are 10 elements that can be saved in the entire array score. Therefore, the memory occupied in the above example has a total of 4*10=40 bytes
Access to arrays
Methods of representing elements in arrays
If you want to access the elements in the array, you can use the index to complete it. The index number of Java's array starts from 10. Taking a shaping array of score[10] as an example, score[0] represents the first element.
Keep going down, the last one is score[9]
Get the length of the array
Getting the length of the array (that is, the length of the array element) in java can be done using the array name.length.
Array name.length--Returns data of type int
package com.qn.array;public class Test { public static void main(String[] args) { int score[]=null;//Declare array score=new int[3];//Develop space, size 3 System.out.println(score.length); }}result
Static initialization of arrays
The previous arrays used dynamic initialization. All contents are not specified when the array is declared, but appear in the form of default values.
Static initialization refers to specifying specific content to the array directly after the array is declared.
If you want to assign the initial value to the array directly during declaration, you can use braces to complete it. Just add the initial value assignment after the life format of the array.
Data type array name[]={initial value 0, initial value 1, initial value 3,...initial value n};
package com.qn.array;public class Test { public static void main(String[] args) { int score[]={1,2,3,4,5,6};//Declare the array System.out.println(score.length); }}result
6
Example to find the maximum and minimum values in an array
package com.qn.array;public class Test { public static void main(String[] args) { int score[]={45,12,8,23,89,56};//Declare the array with static initialization int max=0; int min=0; max=min=score[0]; for (int i = 0; i < score.length; i++) { if(score[i]>max){ max=score[i]; } if(score[i] min=score[i]; } } System.out.println("Maximum:"+max); System.out.println("min:"+min); }}result
Example sorting, sorting is more commonly used in operations
From large to small
package com.qn.array;public class Test { public static void main(String[] args) { int score[]={45,12,8,23,89,56};//Declare array with static initialization int temp=0; for (int i = 0; i < score.length; i++) { for (int j = 0; j < score.length-1; j++) { if(score[i]>score[j]){ temp=score[i]; score[i]=score[j]; score[j]=temp; } } } for (int i = 0; i < score.length; i++) { System.out.print(score[i]+"/t"); } }}result
Don't be confused by the i value at this time if(score[i]>score[j]){
This step is mainly knowledge for comparison. In fact, after completion, the output is sorted according to the value of j.
Two-dimensional array
If a one-dimensional array can be regarded as a line shape in geometry, then a two-dimensional array is equivalent to a table
AB
1 Name Age
2 Qining 21
3 Qi Yan 23
4 Qiwei 26
The way of declaring two-dimensional arrays is similar to that of a thought array, and the keyword new is used to allocate memory.
In fact, the format of declaring and allocating memory is as follows
Dynamic initialization
Data type array name[][];
Array name = new data type [number of rows] [number of columns];
Declare and initialize the array
Data type array name[][]=new Data type [number of rows][number of columns];
Static initialization
Storage of two-dimensional arrays
Declare two-dimensional array score and open up a memory space at the same time
int score[][]=new int[4][3];
The elements that can be saved by the overall data score are 4*3=12. In Java, the space occupied by the int data type is 4 bytes, so the memory occupied by the shaping array is 4*12=48 bytes.
case
package com.qn.array;public class test1 { public static void main(String[] args) { int score[][] = new int[4][3]; score[0][1] = 30; score[1][0] = 31; score[2][1] = 32; score[2][2] = 33; score[3][1] = 34; score[1][1] = 35; for (int i = 0; i < score.length; i++) { for (int j = 0; j < score[i].length; j++) { System.out.print(score[i][j]+"/t"); } System.out.println(""); } }}result
Static initialization of two-dimensional arrays
Only when used will open up space, but not when used (red)
Multidimensional array
Usually only two-dimensional arrays are used
A simple understanding of three-dimensional arrays
package com.qn.array;public class test1 { public static void main(String[] args) { int score[][][]={ { { { { { { { { { { { { { { 1},{6,7} } }, { { { 9,4},{8,3} } } }; System.out.print(score[0][0][0][0]+"/t"); System.out.print(score[0][0][1]+"/t"); System.out.print(score[1][0][0]+"/t"); System.out.print(score[1][0][1]+"/t"); System.out.print(score[1][1][0]+"/t"); System.out.print(score[1][1][1]+"/t"); }}The above definition and usage method of arrays in Java (recommended) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.