Array: is a set of related variables
An array is a collection of related data, and an array is actually a series of variables, which can be divided into one-dimensional arrays, two-dimensional arrays, multidimensional arrays according to usage.
The data is a bit
Do not use an array to define 100 shaping variables: int i1;int i2;int i3
Use an array to define an int i[100];
Array definition: int i[100]; just a pseudo-code, just a means of meaning
One-dimensional arrays
A one-dimensional array can hold tens of thousands of data, and the type of the data is exactly the same,
Using a Java array, you must go through two steps, declaring the array and allocating memory to the array,
Declaration Form One
Declaring a one-dimensional array: Data type array name []=null;
Non-matching memory to array: Array name =new data type [length];
Declaration Form II
Declaring a one-dimensional array: data type [] array name =null;
Java data types fall into two main categories
Basic data types
int, long operation itself is the specific content
Reference data types: arrays, classes, interfaces
Reference is passed a memory right, a piece of memory space, may have more than one person to use 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 array element, and is commonly shaped, floating-point, and character-type.
The array name is used to unify the name of this set of elements of the same data type, with the same naming rules and variables
After an array declaration, the name of this array is actually saved in the stack memory, and the memory required to configure the array in the heap memory is given, and the invariant is to tell the compiler how many elements are to be stored in the declared array, and new is the command compiler based on the length in parentheses
Basic data type even read has its default value: int 0; As long as the reference data type default value 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 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
Array operation, in the stack memory is always the name of the array, only open up the stack of space, the array is never used, must have a point to the memory can be used, to open up new memory space must use the "new" keyword, then the memory will be used to the corresponding stack memory, And a heap of memory space can be at the same time by multiple stacks of memory space point, such as a person can have multiple names, people are equivalent to memory, the name is equivalent to stack memory
Allocating memory space while declaring an array
Declares an array of simultaneous non-matching memory
Data type array name []=new data type [number]
int score[]=new int[10];
Declares an score array with a number of elements of 10, while opening up dependent on memory space durations
In Java, because the shaping data type occupies 4 bytes, the entire array score can hold 10 elements. So the memory consumed in the example above is 4*10=40 bytes
Access to arrays
How elements in an array are represented
To access the elements in the array can be done using the index, the Java Array index label starting from 10, with a score[10] shape array as an example, Score[0] represents the first element
Always down, the last one for Score[9]
Gets the length of the array
The length of the array in Java (that is, the length of the array elements) can take advantage of the array name. Length Complete,
The array name. length--Returns a data of type int
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
System.out.println (score.length);
}
}
Results
Static initialization of arrays
The previous array, which is used for dynamic initialization, is not specified at the time of the array declaration, but appears as 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 declaration, you can use curly braces, as long as the array's life format is followed by an assignment of the initial values.
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};//declaring an array with static initialization
System.out.println (score.length);
}
}
Results
6
Example finding 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};//declaring an 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 value:" +max);
SYSTEM.OUT.PRINTLN ("Minimum Value:" +min);
}
}
Results
Example sorting, sorting in action is more commonly used
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};//declaring an 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");
}
}
}
Results
Don't be fooled by the I value at this time if (Score[i]>score[j]) {
This step of the main knowledge in order to compare, actually finished after the output is based on the value of J sort
Two-dimensional arrays
If you can think of a one-dimensional array as a line shape in the geometry, then the two-dimensional array is equivalent to a table
A B
1 Name Age
2 qining 21
3 Jiyan 23
4 Quanrixue 26
A two-dimensional array is declared in a similar way to an array, and memory allocation is the same as using the New keyword
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];
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 a two-dimensional array score simultaneously opening up a memory space
int score[][]=new int[4][3];
The overall data score can be saved 4*3=12, in Java, the INT data type occupies 4 bytes, so the shaping array takes up a total of 4*12=48 bytes of memory
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 ("");
}
}
}
Results
Static initialization of two-dimensional arrays
When used, it will open up space, not the (red part) does not open up space
Multidimensional arrays
It's usually just 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}
},
{
{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");
}
}
Arrays in Java