Java Basics (five) array

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_44779847/article/details/102732061

Array:
1) the same data element type set
2) is a data type (reference type)
to define arrays. 3):
int [] = ARR new new int [10];
initialization 4) of the array:
int [] = new new ARR int [. 4]; // 0,0,0,0
int [] = {1,4,7,9} ARR; 1,4,7,9 //
int [] = ARR new new int [] {. 1, } 4,7,9; 1,4,7,9 //
int [] ARR;
ARR = {1,4,7,9}; // compile error, this embodiment only declare also initializes
arr = new int [ ] {1,4,7,9}; // correct
5) to access the array:
5.1) to obtain the length of the array (by elements (array name .length) number)
int [] = ARR new new int [. 3] ;
System.out.println (arr.length); //. 3
5.2) to access elements in the array by the subscript / indexing
index from 0, up to (array length -1)
int [] = new new int ARR [. 3];
arr [0] = 100; // to the first element of arr assigned the 100
arr [. 1] = 200 is; // arr to the first element 2 is assigned 200 is
arr [2] = 300; / / to the third element of arr assigned to 300
// arr [. 3] = 400; // array subscript bounds exception
System.out.println (arr [arr.length-1] ); // output value of the last element
6) through the array:
int [] = ARR new new int [10];
for (int I = 0; I < arr.length; I ++) {
ARR [I] = 100;
}
for (int I = 0; I <arr.length; I ++) {
System.out.println (ARR [I]);
}
for (int I = ARR . 1-.length; I> = 0; I -) {
System.out.println (ARR [I]);
}
. 7) copies of arrays:
7.1) System.arraycopy (a,. 1, a1,0,4);
7.2) int [] A1 = Arrays.copyOf (a,. 6);
a = Arrays.copyOf (a, a.length +. 1); // expansion
sort 8) array:
8.1) Arrays.sort (ARR); / / ascending

Guess you like

Origin blog.csdn.net/qq_44779847/article/details/102732061