Part I: Array
An array is a collection of data, each data element in the array is called.
1.1: What can be stored in the array
It can be stored in any type of array elements, but can only store an array of elements of the same type.
1.2: length of the array
1, you need to specify the length of the array at the time of creation
2, the length of the array has been determined can not be modified
1.3: the array index
1, a method for accessing the array data of each index allocated specifically to each element of the array (automatically assigned)
2, the subscript of the array is zero
3, like an array index must be less than the length of the array
Question: What is the maximum value of the underlying array?
A: length of the array -1 (length-1)
1.4: Create an array of ways
grammar:
Data Type [] = new variable name Data type [length of the array];
eg:
int[] ages = new int[10];
Explanation: The statement on behalf of an array of integers, a variable named ages, and assigned to the variable ages, and assigned a new length of the array out of 10.
Note: Each element in the new array will have a default value.
Data Type [] variable name = value, value, value ...}
eg: boolean[] genders = {true,false,true,false};
Explanation: declare an array of boolean class, the variable named genders, and assigned to the variable genders, the four data assigned to the variable named genders array.
Note: This time the length of the array has been fixed genders, genders each element of the array has a value.
Data Type [] = new variable name Data type [] {value, value, value ...}
eg:String[] args = new String[]{"tom","lisi","wangwu"}
Explanation: Declare a string array of type and assignment, created a new array, the new array is initialized values. The new array is assigned to the args
Note: The length of time this has been fixed args array
1.5: assign an array of ways
1, created when assignment:
int[] arg = new int[]{1,2,4,5,7,8,2,3,4}
2, when creating the assignment:
int[] arg = {1,2,3,4,5,6,2,3,4};
3, using the subscript assignment: the most commonly used
eg:
int [] arg = new int [10]; // no empty array assignment unit 10
arg [0] = 189; // 189 into data representative of the position number 0 of the array arg
arg [5] = 12; // data representative of the number 12 into location 0 of the array arg
Note: The newly created array for each value will default initialization
byte/short/int/long 0
float 0.0f
double 0.0d
Reference data types null
char '\u0000'
boolean false
1.6: Value of an array of ways
Use index values:
int[] args = new int[]{22,3,4,555,62,86};
1, taken args an array index value 3
int age = args [3]; // args [3] is to get the underlying value at No. 3, where it is assigned to the late age for use.
2, taking the value of the array index args 9
int tomage = args[9];
3, to the array index value args 10
int lisiage = args[10];
Why is this index value of 10, did not get?
1.7: traversing the array output
Use for traversing output + subscript
1.8: Multidimensional Arrays
Data on each element of the array is an array, so you get a multidimensional array.
1, declare a multidimensional array.
Data Type [] [] args = new data type [length of the array] [];
Data Type [] [] args = {
{"mike","jane"},
{"Mary"},
{"Tom","Jerry"}
}
2, two-dimensional array assignment:
int[][] args = new int[5][];
args[0] = new int[3];
args[1] = new int[2];
args[3] = new int[2];
args[0][0] = 1;
args[0][1] = 1;
args[4][1] = 1;
args[4][2] = 2;
3, two-dimensional array of values:
int age = args[0][0];
Second part: ... the array copy system using a class System
java.lang.System
System.arraycopy(...);
Once you determine the length of the array can not adjust, we can work to achieve change in length of the array by copying the contents of the array. Providing an auxiliary System class
arraycopy method provides functionality to copy the contents of the array:
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
Parameter 1, the target needs to be replicated array
Parameter 2, start copying from a position that this array
3 parameters, need to copy the data to another that a new array object
Parameter 4, which is copied to a new position inside the array (start counting from this location)
5 length parameter, an array of replicated
Part III: Arrays of use
Arrays.sort();
Arrays.toString();
Part IV: randomly at a target value in the array.
Math.random();
Part V: Sort the array
Bubble Sort:
Part VI: variable parameters
Syntax: String ... args
Note: The only variable parameter is the last parameter list.