2018-02-28 array: A combination of data of the same data type.
such as: int score1=1;
int score2=2;
int score3=3;
Using arrays:
1. Initialization of arrays
Int[] score1=new int[]{1,2,3};// static initialization: The array is declared and initialized at the same time as the corresponding element assignment operation for the array.
Int[] score2=new int[3];// dynamic initialization : Separate the declaration initialization array from the element assignment operation to the corresponding element of the array.
Score2[0]=1;
score2[1]=2;
score2[2]=3;
To declare an array of incorrect notation:
- String[] names=new string[5]{"AA", "BB", "CC"};
- int i[10];
- int i=new int[];
Be sure to indicate the length of the array at creation time, regardless of dynamic initialization or static initialization.
2. How to apply an array element: The way the bottom corner of an array is labeled. The lower corner mark starts at 0 and ends at n-1. where n is the length of the array.
3. Length of the array: called by the property length of the array.
System.out.println (score2.length);//3
4. How to iterate through an array
For loop
for (int i=0;i<score1.length;i++) {
System.out.println (Score1[i]);
}
5. Default initialization values for arrays:
1) byte int short long 0
2) Float double 0.0
3) Char Space
4) Boolean false
5) Reference type variable null
6. Memory structure of the array
The basic structure of memory: stack space , heap space , method area , static domain
The memory structure of the array: in the case of an array: ① the corresponding reference placed in the stack space ② array of entities stored in the heap space
Scope of the array: reference counting algorithm: If the value of the heap space is not referenced in the stack space, then the JVM will reclaim it in memory.
Two-dimensional arrays
1. Declaring and initializing
One-dimensional
Int[] =new int[12];
int[0]=12;
int[] j=new int []{12.3};
Two-dimensional
1) string[][] str=new STRING[4][3];//4 row 3 column length 4 dynamic initialization
2) string[][] str1=new string[4][];
Str1[0]=new String[3];
Str1[1]=new String[4];
...
3) Static initialization
Int[][] Arr=new int[][]{1,2,3}{4,5}{6};
2. How to reference a two-dimensional array
arr[1][0]=12;
3. Length of two-dimensional array: ARR.LENGTH;//3
Arr[1].length;
4. Traversing a two-dimensional array
for (int i=0;i<arr.length;i++) {
for (int j=0;j<arr.length;j++) {
System.out.print (arr[i][j]+ "\ t");
}
System.out.println ();
}
5. Structure of two-D arrays
0000 00000000
0000 000000
0000 0000
6. Common Exceptions to arrays
1) array subscript out of bounds
2) NULL pointer exception
7. Common algorithms for arrays
1. Find the maximum, minimum, and, average, and median values of the array.
2. Copying and reversal of arrays. Value passing
1) Copy
Case one: Make the pointers of two arrays equal
Case two: Re-new an array and assign the elements sequentially.
2) inverse of the array
for (int i=0;i<arr.length/2;i++) {
int temp=arr[i];
Arr[i]=arr[arr.length-1-i];
Arr[arr.length-1-i]=temp;
}
for (int x=0,y=arr.length-1;x<y;x++,y--) {
int temp=arr[x];
Arr[x]=arr[y];
Arr[y]=temp;
}
Expand: Invert string str= "ABCDEFG";
Idea: Put it in an array and apply the algorithm above
Sort the array:
Insert Sort
Direct Insert Sort , binary insert sort, shell sort
Exchange sort
Bubble sort , quick sort (partitioned interchange sort)
Select sort
Simple select sort, heap sort
Merge sort
Base sort
In real development, call the Arrays tool class Arrays.sort (arr);//default from large to small
Java Learning---Arrays