JAVA basics/Lesson 21st: how to use two-dimensional arrays in arrays/JAVA and two-dimensional arrays in java

Source: Internet
Author: User

JAVA basics/Lesson 21st: how to use two-dimensional arrays in arrays/JAVA and two-dimensional arrays in java

I. initialize a two-dimensional array:

This is a one-dimensional array. every element in it is an int of the basic type.

int a[] =new int[]{1,2,3,4,5};

This is a two-dimensional array. every element in it is a one-dimensional array.
Therefore, a two-dimensional array is also called an array:

int b[][] = new int[][]{   {1,2,3},   {4,5,6},   {7,8,9}};
Public class HelloWorld {public static void main (String [] args) {// initialize a two-dimensional array, int [] [] a = new int [2] [3]; // There are two one-dimensional arrays. The length of each one-dimensional array is 3 a [1] [2] = 5; // you can directly access one-dimensional arrays, because space has been allocated // only two-dimensional array int [] [] B = new int [2] []; // There are two one-dimensional arrays, at present, the length of each one-dimensional array is not allocated B [0] = new int [3]; // The length must be allocated in advance to access B [0] [2] = 5; // allocate the int [] [] c = new int [] [] {1, 2, 4}, {4, 5}, {6, 7, 8, 9 }};}}

2. Common JAVA. UTIL. ARRAYS methods:

Arrays is a tool class for Arrays. It can be sorted, searched, copied, and filled. This greatly improves the efficiency of developers.

1. array replication:

Similar to using System. arraycopy for Array copying, Arrays provides a copyOfRange Method for Array copying.
The difference is that System. arraycopy requires that the target array be prepared and the length be allocated. CopyOfRange only needs the source array, and the target array can be obtained through the return value.
In addition, note that copyOfRange's 3rd parameters indicate the end position of the source array, which is not available.

Import java. util. arrays; public class HelloWorld {public static void main (String [] args) {int a [] = new int [] {18, 62, 68, 82, 65, 9 }; // copyOfRange (int [] original, int from, int to) // The first parameter indicates the source array // The second parameter indicates the start position (obtained) // The third parameter indicates the end position (not available) int [] B = Arrays. copyOfRange (a, 0, 3); for (int I = 0; I <B. length; I ++) {System. out. print (B [I] + "");}}}
2. convert to a string:

If you want to print the content of an array, You need to traverse the array one by one through the for loop.
However, Arrays provides a toString () method to directly convert an array to a string, so that you can easily observe the content of the array.

import java.util.Arrays;  public class HelloWorld {    public static void main(String[] args) {        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };        String content = Arrays.toString(a);        System.out.println(content);     }}

3. Sorting:

In the previous chapter, we learned how to sort by choice method and bubble method. The Arrays tool class provides an sort method, which can be sorted with only one line of code.

Import java. util. arrays; public class HelloWorld {public static void main (String [] args) {int a [] = new int [] {18, 62, 68, 82, 65, 9 }; system. out. println ("Before sorting:"); System. out. println (Arrays. toString (a); Arrays. sort (a); System. out. println ("sorted:"); System. out. println (Arrays. toString ());}}

4. Search:

Query where an element appears
Note that before using binarySearch for search, you must use sort for sorting. If the array contains multiple identical elements, the search result is uncertain.

 

Import java. util. arrays; public class HelloWorld {public static void main (String [] args) {int a [] = new int [] {18, 62, 68, 82, 65, 9 }; arrays. sort (a); System. out. println (Arrays. toString (a); // before using binarySearch, you must use sort to sort System. out. println ("position where the number 62 appears:" + Arrays. binarySearch (a, 62 ));}}

5. Check whether the data is the same:

Compare whether the content of the two arrays is the same
The last element of the second array is 8, which is different from the first array. Therefore, the comparison result is false.

import java.util.Arrays; public class HelloWorld {    public static void main(String[] args) {        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };        int b[] = new int[] { 18, 62, 68, 82, 65, 8 };        System.out.println(Arrays.equals(a, b));    }}

6. Fill:

import java.util.Arrays;  public class HelloWorld {    public static void main(String[] args) {        int a[] = new int[10];          Arrays.fill(a, 5);        System.out.println(Arrays.toString(a));    }}

 

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.