/*
* Use of arrays
* 1, data: an orderly combination of the same type of data
* 2, the length of the array is determined, once created will not be able to change
* 3, each element in the array must be the same, cannot appear mixed type
* 4, the data type of the element can be arbitrary, mainly including the basic type and reference type
* 5, the array variable belongs to the reference variable, the array belongs to the object, so the array is stored in the heap, each element corresponds to the object's member variable.
*
* Note: The array does not allocate heap space at the time of declaration, it does not need to specify the length, and the JVM allocates heap space when the array is instantiated, so the array length needs to be specified at this time
*/
import java.util.Arrays;;
public class Array_4_21_01 {
public static void Main (string[] args) {
Int[] array1; //Declare an array variable without specifying a length
Array1 = new INT[10]; Array instantiation, specifying length required
System.out.print ("One-dimensional array");
for (int i = 0; i < array1.length; i++) {
Array1[i] = i;
System.out.print (array1[i]+ "");
}
System.out.println ("");
System.out.println (arrays.tostring (array1));
System.out.println ("-------------------------------");
/*
* element is an array of reference types
*/
Man[] Mans; //Declare an array variable of a man reference type
Mans = new MAN[5]; //Instantiate an array of this man reference type
Man m1 = New Mans (1, 27);
Man m2 = new Mans (2, 28);
Mans[0] = M1;
Mans[2] = m2;
System.out.println ("Mans array Value is" +arrays.tostring (Mans));
Static initialization
Int[] array2 = {2,5,9};
Man[] Mans2 = {
New Man (1,29),
New Man (2,30)
};
Dynamic initialization
int[] Array3 = new int[2];
Array3[0] = 1;
ARRAY3[1] = 2;
System.out.println ("Array3 array value is" +arrays.tostring (ARRAY3));
The default initialization of an array, because the elements of the array are equivalent to the instance variables of the class, so when the array is allocated space, each element is implicitly initialized as an instance variable.
/*
*int[] A = new int[2]; Initialize to [0, 0]
*boolean[] b = new Boolean[2], initialize to [False, false]
*string[] s = new string[2], initialized to [null, NULL]
*/
Copy of the array, using System.arraycopy this static method, System.arraycopy (original array, original array start position, target array, target array start position, copy how many elements);
String[] S1 = {"Microsoft", "IBM", "Sun", "Oracle", "Apple"};
string[] s2 = new STRING[5];
System.arraycopy (S1, 0, S2, 0, s1.length);
System.out.println ("S2 This array is" +arrays.tostring (S2));
Iterating through an array with an enhanced for loop
for (String e:s2) {
System.out.println (e);
}
Array sorting, mainly according to the element's application type and the basic type are divided into two kinds, the element is the reference type of the array ordering we later combine the container to understand
Binary Method Search
Int[] Array4 = {1,3,232,23,543,12,59};
Arrays.sort (ARRAY4); before using the binary method, you must first sort the array
SYSTEM.OUT.PRINTLN ("The index of this element is" +arrays.binarysearch (array4,12));
Fill
Arrays.fill (Array4, 1, 4,100); //Set array Array4 from index position 1 to 3-bit padding to 100
System.out.println (arrays.tostring (ARRAY4));
Aslist (with container understanding)
/*
* Multidimensional arrays, the declaration and initialization of multidimensional arrays are from low-dimensional to high-dimensional order
*/
Static initialization of two-dimensional arrays
Int[][] Array5 = {
{A-i},
{3,4},
{2,5,8,9}
};
System.out.println ("Array5" The one-dimensional array value of this two-dimensional array is "+arrays.tostring (Array5[0]));
For (int[] e:array5) {
for (int f:e) {
System.out.println (f);
}
}
Dynamic initialization of two-dimensional arrays
int[][] Array6 = new int[3][];
Array6[0] = new int[]{1,2,3};
ARRAY6[1] = new int[]{3,4};
ARRAY6[2] = new int[]{2,5,8,9};
System.out.println (the "Array6" value of this array [2][3] is "+array6[2][3]");
Gets the first-dimensional array length of the two-dimensional array is array6.length; Array6[0].length is the length of the first array of two-dimensional arrays
}
}
Class Man {
private int age;
private int id;
Public Mans (int id,int age) {
Super (); //
This.age = age;
This.id = ID;
}
}
/* Thinking questions
* 1, why do you need the concept of arrays?
*
* 2, say the array of three initialization methods.
*
* 3. Is the length of the array variable?
*
* 4. What class method is used for copying arrays?
*
* 5, talk about the use of arrays class
*
*/
This article is from the "shadow debut" blog, please be sure to keep this source http://woodywoodpecker.blog.51cto.com/4820467/1636698
An array of Java