Java entry (6): array and java entry Array

Source: Internet
Author: User

Java entry (6): array and java entry Array

An array is a data structure used to store a set of values of the same type. It can also be seen as a group of data with the same data type.

I. syntax and declaration Array

1. Syntax: Data Type [] array = new data type [length];

Note:

When an array is created using the length, each element has a default value.

For int, byt, short, and long, the default value is 0;

The default value of double and float is 0.0;

The default value of boolean is false;

For char, the default value is 0 characters;

For the reference type (including String), the default value is null.

2. If you already know the elements of the array, you can use the elements to initialize the array without writing the array length. The number of elements is the array length. Second, for example, int [] a and int a [] statements are available, but we do not recommend using the subsequent statement.

3. If there are not many data elements and the values of each element are known, you can directly use the elements for initialization. If there are too many elements, use the length to create an array. If the element does not have an initial value, use the length to initialize it.

/*** Array declaration */public static void testMethod () {// first writing method int [] array = new int [3]; // The default value of int type is 0 System. out. println (array [0]); // 0 String [] strArray = new String [3]; // reference type (including String). The default value is null System. out. println (strArray [0]); // null // The second method of array declaration int [] array2 = {1, 2, 3}; System. out. println (array2 [0]); // 1 // This method can also be used, but it is not recommended that [] be followed by the data type, int array3 [] = {4, 5, 6}; System. out. println (array3 [0]); // 4}
Ii. Accessing array elements

1. Use the array name [subscript serial number] to access array elements.

2. The. length attribute of the array to obtain the length of the array, that is, the number of array elements.

3. The range of array subscript ranges from 0 to length-1.

4. If the access is out of range, an exception occurs and the array subscript is out of range. Subscripts smaller than 0 or greater than or equal to length are out of range.

/*** Get the array length, element access */public static void arrayLength () {// get the length of the array, use. length attribute int [] array = {1, 2, 3, 4}; System. out. println (array. length); // 4 // the length of an array cannot be changed once defined, because its length attribute is modified by the final keyword and cannot be modified. // array. length = 5; // The final field array. length cannot be assigned // access the elements of the array. Use the [subscript of the element]. The subscript starts from 0. out. println (array [0]); // print the first element of the array. lang. arrayIndexOutOfBoundsException exception // System. out. println (array [4]);}
3. array Traversal

With the for loop and for each loop, you can easily traverse array elements.

/*** Array traversal */public static void seeArrayElements () {int [] array = {1, 2, 3, 4, 5 }; // print for (int I = 0; I <array. length; I ++) {System. out. println (array [I]);} // for each print, the first parameter in the brackets is the data type of the element // The second parameter is the name of the local variable for obtaining the value (which can be named at will), and the third parameter is the array name (int element: array) {System. out. println (element );}}
Iv. Copy and resize Arrays

1. Copy an array.

Use java. util. the copyOf () method in the Arrays class is implemented, while the Arrays. the implementation principle of the copyOf () method is to use the arraycopy method of the System class. If you are interested, you can try to build your own wheels and then compare it with the official arraycopy method.

2. resize the array.

The length of an array cannot be modified after initialization. To increase or decrease the length of an array, you can also use the Arrays. copyOf () method. No matter whether the array is expanded or reduced, the original array will not change, but the object is changed into a new array.

/*** Copy the array and resize the array. no matter whether the array is scaled up or down, the original array will not change */public static void makeArrayCopy () {// copy the array, java is used here. util. the copyOf () method in the Arrays class int [] array = {2, 4, 6}; int [] newArray = Arrays. copyOf (array, array. length); // determines whether two Arrays are equal. The equals () method System in the Arrays class is used. out. println (Arrays. equals (array, newArray); // true // array expansion int [] addLength = Arrays. copyOf (array, array. length + 1); System. out. println (addLength. length); // 4 for (int e: addLength) {System. out. println (e); // print 2 4 6 0} in sequence // The array becomes smaller int [] addleng2= Arrays. copyOf (array, array. length-1); System. out. println (addLength2.length); // 2 for (int e: addlength1) {System. out. println (e); // print 2 4 in sequence }}
5. array sorting

Compare the Adjacent Elements of the array, change the positions according to the ascending or descending rules, and finally obtain the sorted array.

/*** Sort arrays in ascending order */public static void sortArrayAsc () {// declare and initialize an array int [] array = {2, 10, 3, 78, 1, 29, 91, 5}; System. out. println (Arrays. toString (array); for (int I = 0; I <array. length-1; I ++) {for (int j = 0; j <array. length-i-1; j ++) {if (array [j]> array [j + 1]) {int k = array [j]; array [j] = array [j + 1]; array [j + 1] = k ;}} System. out. println (Arrays. toString (array);}/*** sort the array in descending order */public static void sortArrayDesc () {// declare and initialize an array int [] array = {2, 10, 3, 78, 1, 29, 91, 5}; // convert the array into a string and output the System. out. println (Arrays. toString (array); for (int I = 0; I <array. length-1; I ++) {for (int j = 0; j <array. length-i-1; j ++) {if (array [j] <array [j + 1]) {int k = array [j]; array [j] = array [j + 1]; array [j + 1] = k ;}} System. out. println (Arrays. toString (array);}/*** use Arrays. sort the sort () method */public static void utilSort () {// declare and initialize an array int [] array = {2, 10, 3, 78, 1, 29, 91, 5}; System. out. println (Arrays. toString (array); Arrays. sort (array); System. out. println (Arrays. toString (array ));}
Vi. Two-dimensional array

A two-dimensional array is essentially a one-dimensional array. operations on a two-dimensional array are actually operations on a one-dimensional array.

1. Declaration. Type [] [] variable name = new type [length] [length];

2. length attribute. The length is the length value in the first.

3. The subscript also starts from 0 and follows the rule that the subscript cannot cross the border.

4. Other operations are similar to operations on one-dimensional arrays.

Public static void stateTwoDimArray () {// declare a two-dimensional array int [] [] array = new int [2] [3]; // assign values to the element array [0] [0] = 1; array [0] [1] = 2; array [0] [2] = 3; System. out. println (Arrays. toString (array [0]); // [1, 2, 3] System. out. println (Arrays. toString (array [1]); // [0, 0, 0] System. out. println (array. length); // 2 // declare and initialize a two-dimensional array int [] [] array2 ={{ 1, 2}, {3, 4}; System. out. println (Arrays. toString (array2 [0]); // [1, 2] System. out. println (Arrays. toString (array2 [1]); // [3, 4] System. out. println (array2.length); // 2}

The above code has been uploaded to GitHub. The address is GitHub! If you have any questions, please leave a message below.

 

The article was first published on my personal public account:Happy book. I like to share my songs, movies, books, code, and deep-night meditation. Looking forward to your attention!

Enter the keyword"Java learning ebook", You can get 12 books related to Java learning. If the financial ability permits, please also support the author's genuine and paper books, which is not easy to create.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.