Categories
Uncategorized

Java Road from entry to advanced of (E)

Previous article, we introduced the Java looping constructs, in this chapter we look at the array of Java

One array for each programming language for data structures are important, of course, are not the same in different languages ​​and to achieve processing array.

Array

– a set of elements of the same type of data

– elements arranged in linear order. The so-called linear order is defined in the first outer element, each element has a unique precursor elements, except for the last element, each element has a unique subsequent element (with a a)

– can be done by a sequence number (index) identifying the position of the element to access each element (index is 0, the maximum number of elements to -1)

Creating an array

 1 public class HelloWorld {
 2     public static void main(String[] args) {
 3         //

The first: to declare an array variable arr, and then assign an array of references to the newly created variable arr

4 int[] arr; 5 arr = new int[5]; 6 7 //

The second: declare an array of variables, and arrays can be used to create a complete statement

8 int[] arr1 = new int[5]; 9 } 10 }

Tips: Enter under IDEA fori then return editor can generate for (int i = 0; i <; i="" ++)="" {},="" then="" the="" transport="" will="" be="" able="" to="" enter="" foreach="" generated="" for="" (:)="" {}<="" p=""/>

In the above code, we declare the array arr, but did not give a value, then the system will automatically assigned to each array element, the default is 0, we can assign to each element can be assigned at the time of the array initialization , the following code:

 1 public class HelloWorld {
 2     public static void main(String[] args) {
 3         //

Declare an integer array variable arr,

4 int[] arr = new int[5]; 5 for (int i = 0; i < arr.length; i++) { 6 //

The default value is 0 array arr

7 System.out.println(arr[i]); // 0 0 0 0 0 8 } 9 10 //

Declare an integer array variable arr1,

11 int[] arr1 = new int[5]; 12 arr1[0] = 0; 13 arr1[1] = 1; 14 arr1[2] = 2; 15 arr1[3] = 3; 16 arr1[4] = 4; 17 for (int i = 0; i < arr1.length; i++) { 18 //

Arr1 array of output value

19 System.out.println(arr1[i]); // 0 1 2 3 4 20 } 21 22 //

Declare an integer array variable arr2, arr12 assignment [0,1,2,3,4]

23 int[] arr2 = {0, 1, 2, 3, 4}; 24 for (int i = 0; i < arr2.length; i++) { 25 //

Output array value arr2

26 System.out.println(arr2[i]); // 0 1 2 3 4 27 } 28 } 29 }

We use the above code output from the loop array for each element, we can use for-each output:

1 public class HelloWorld {
2     public static void main(String[] args) {
3         //

Declare an integer array variable arr,

4 double[] arr = {0.1, 1.2, 2.3, 3, 4}; 5 for(double i : arr){ 6 System.out.println(i); // 0.1 1.2 2.3 3.0 4.0 7 } 8 } 9 }

We can also copy elements from one array into another, as follows:

 1 public class HelloWorld {
 2     public static void main(String[] args) {
 3         int[] arr1 = {0, 1, 2, 3, 4};
 4         int[] arr2 = new int[5];
 5         /**
 6          * arr1:源数组
 7          * 1:源数组起始下标
 8          * arr2:目标数组
 9          * 0:目标数组的起始下标
10          * 4:要复制的元素的个数
11          * */
12         System.arraycopy(arr1, 1, arr2, 0, 4);
13         for (int i = 0; i < arr2.length; i++) {
14             System.out.println(arr2[i]); // 1 2 3 4 5 0
15         }
16     }
17 }

Replication is of course not exceed the range, for example, we define the length arr2 3, i.e., int [] arr2 = new int [3];, then the copy number has to be longer than the target array, the program will be given.

The length of the array is immutable after it is created, but we can expand it, which means creating a larger new array and copying the contents of its original array into it, which can be achieved by Arrays.CopyOf () method.

 1 public class HelloWorld {
 2     public static void main(String[] args) {
 3         int[] arr1 = {0, 1, 2, 3, 4};
 4         //

arr1 expansion source array arr1.length + 1 array length

5 arr1 = Arrays.copyOf(arr1, arr1.length + 1); 6 for (int i = 0; i < arr1.length; i++) { 7 System.out.println(arr1[i]); // 0 1 2 3 4 0 8 } 9 10 } 11 }

Of course, we may be the age of the original volume reduction array, the array need only expansion can be smaller than the source array, then the end of the source array extra elements will be removed.

java provides an easy way for us to sort the array, Arrays.sort ()

 1 public class HelloWorld {
 2     public static void main(String[] args) {
 3         int[] arr = {1,4,5,67,23,3};
 4         Arrays.sort(arr);
 5         for (int i = 0; i < arr.length; i++) {
 6             System.out.println(arr[i]); // 1 3 4 5 23 67
 7         }
 8 
 9     }
10 }

 

Leave a Reply