Array: is a set of related variables
An array is a set of related data, an array is actually a series of variables, the array can be divided into one-dimensional array, two-dimensional array, multidimensional array
The data is a bit
Do not use an array to define 100 integer variables: int i1;int i2;int i3
Use array definition int i[100];
Array definition: int i[100]; it's just a pseudo code, just an expression of meaning.
One-dimensional arrays
A one-dimensional array can hold tens of thousands of data, and the type of data is exactly the same,
Using a Java array, you must go through two steps to declare the array and allocate memory to the array.
Declaration Form One
Declares one-dimensional array: data type array name []=null;
Non-matched memory to array: Array name =new data type [length];
Declaration Form Two
Declare one-dimensional array: data type [] array name =null;
Java data types fall into two broad categories
Basic data types
The int, long operation is the specific content
Reference data types: arrays, classes, interfaces
Refers to the use of a memory, a memory space, may have more than one person at the same time
Case Declaration Array
Package Com.qn.array;
public class Test {public
static void Main (string[] args) {
int score[]=null;//declaration array
score=new int[3];//open Space , size 3
}
}
In the declaration format of an array, the data type is the data type of the element of the array, which is commonly used for shaping, floating point type, and character type.
An array name is the name used to unify this set of elements of the same data type, with the same naming rules and variables
The array declaration actually saves the name of the array in stack memory, the knot is the memory required to configure the array in heap memory, which tells the compiler how many elements the array is to hold, and new is the command compiler according to the length of the brackets
The base data type is occasionally read with its default value: int 0; As long as the default value of reference data type is NULL
Case
Package Com.qn.array;
public class Test {public
static void Main (string[] args) {
int score[]=null;//declaration array
score=new int[3];// Open space, size of 3
System.out.print ("score[0]=" +score[0]);
System.out.print ("\tscore[1]=" +score[1]);
System.out.print ("\tscore[2]=" +score[2]);
}
Explanation of stack memory
In the array operation, the name of the array is always stored in the stack memory, only open up the space inside the stack, the array is never used, must have pointed to the memory can be used, to open up new to the memory space must be used to the next keyword, after that is the use of memory to the corresponding stack memory, And a heap memory space can be pointed at the same time by a number of stack memory space, such as a person can have multiple names, people are equivalent to memory, the name is equivalent to stack memory
Allocating memory space at the same time as declaring an array
To declare an array at the same time as not with memory
Data type array name []=new data type [number]
int score[]=new int[10];
Declare an array of score with an element number of 10, while opening up the use of the time dependent on memory space
In Java, because the shape data type occupies 4 bytes, the entire array score 10 of the elements that can be saved. So the memory consumed in the above example has a total of 4*10=40 bytes
Access to arrays
Representation of elements in an array
To access the elements in the array can be done using the index, Java array index numbering starting from 10, with a score[10] of the plastic array as an example, Score[0] represents the first element
Always down, last one for score[9]
Gets the length of the array
The length of the array (that is, the length of the array element) can be obtained in Java by taking advantage of the array name. Length Completed,
The array name. length--returns an int type of data
Package Com.qn.array;
public class Test {public
static void Main (string[] args) {
int score[]=null;//declaration array
score=new int[3];//open Space With a size of 3
System.out.println (score.length);
}
}
Results
Static initialization of arrays
The previous arrays are all dynamic initialization, and all content is not specified at the time of the array declaration, but in the form of a default value
Static initialization refers to specifying the specific contents of an array directly after it is declared
If you want to assign an initial value to an array directly at the time of the declaration, you can do it with curly braces, as long as you add the initial value to the array's life format,
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};//use static initialization to declare an array
System.out.println (score.length);
}
Results
6
Example to find the maximum and minimum values in the array
Package Com.qn.array;
public class Test {public
static void Main (string[] args) {
int score[]={45,12,8,23,89,56};//use static initialization to declare an array
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 ("Minimum:" +min);
}
Results
Example sort, which is more commonly used in operations.
From big to small
Package Com.qn.array;
public class Test {public
static void Main (string[] args) {
int score[]={45,12,8,23,89,56};//use static initialization to declare an array
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");}}
Results
This time don't be fooled by the I value if (Score[i]>score[j]) {
The main knowledge of this step is to compare, actually after the output is based on the value of J to sort
Two-dimensional array
If a one-dimensional array can be used as a line graph in geometry, then a two-dimensional array is equivalent to a table
A B
1 Name Age
2 qining 21
3 Jiyan 23
4 Ziwi 26
A two-dimensional array is declared in the same way as an array, and memory allocations are the same as the New keyword
In fact, the Declaration and allocation of memory format is as follows
Dynamic initialization
Data type array name [];
Array name =new data type [number of rows] [number of columns];
Declaring and initializing an array
Data type array name [][]=new data type [number of rows] [number of columns];
Static initialization
Storage of two-dimensional arrays
Declaring two-dimensional array score and opening a memory space
int score[][]=new int[4][3];
Overall data score the element that can be saved is 4*3=12, in Java, the INT data type takes up 4 bytes of space, so this shaping array occupies a total of 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] =;
Score[1][0] =;
SCORE[2][1] =;
SCORE[2][2] =;
SCORE[3][1] =;
SCORE[1][1] =;
for (int i = 0; i < score.length. i++) {for
(int j = 0; J < Score[i].length; J +) {
System.out.print (Scor e[i][j]+ "\ t");
}
System.out.println ("");}}}
Results
Two-dimensional array static initialization
The use of the time will open up space, do not use (the red part) does not open up space
Multidimensional arrays
Usually just use a two-dimensional array
Three-dimensional array simple to understand
Package Com.qn.array;
public class Test1 {public
static void Main (string[] args) {
int score[][][]={
{
{5,1},{6,7}
},< c10/>{
{9,4},{8,3}}}
;
System.out.print (score[0][0][0]+ "T");
System.out.print (score[0][0][1]+ "T");
System.out.print (score[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 of the Java array and the use of the method (recommended) is to share all the content of the small, hope to give you a reference, but also hope that we support the cloud-dwelling community.