Related knowledge of arrays

Source: Internet
Author: User

array Default value comparison chart

Array type Default values for array elements
BYTE, short, int, long 0
float, double 0.0
Char ' + '
Boolean False
Object.type Null

Related Operations for arrays:

1. Array length

Syntax format: array name. length

2. Array padding

Syntax format: Arrays.fill (array name, value) Arrays.fill (array name, start subscript, end subscript, value)

Note: When assigning to arrays using the Fill method of arrays, you can specify the starting position begin and end position end, which will only assign to the array elements in the following table for the [begin,end] interval.

3. Array replication

Syntax format: Arraycopy (array A, start copying subscript, copy to array B, start copy subscript, copy length)

4. Array comparison

Syntax format: arrays.equals (array 1, array 2) return value to Boolean type

5. Array sorting

Syntax format: Arrays.sort (Array)

6. Finding array elements

Syntax format: arrays.binarysearch (array) binary lookup operation

ImportJava.lang.reflect.Array;Importjava.util.Arrays; Public classJavaTest2ImplementsJavaTest1 { Public Static voidMain (string[] args) {int[][]a={{1,2,3},{4,8},{5,7,6}}; int[][]b={{1,2,3},{1,2,3,4},{12,12,1}}; int[]c={1,2,5,4,3}; int[]d=New int[5]; //traversing array a     for(int[] is:a) { for(intI:is) {System.out.print (i+" "); }            }    //to sort the C arrayArrays.sort (c);  for(inti = 0; i < c.length; i++) {System.out.print (C[i]+"  ");        } System.out.println (); //comparison of two arraysSystem.out.println (Arrays.equals (A, b)); //array for numeric lookupSystem.out.println (Arrays.binarysearch (C, 4)); //Array ReplicationSystem.arraycopy (c, 1, D, 1, 4);  for(inti:d) {System.out.print (i+"  ");        } System.out.println (); //array paddingArrays.fill (d, 0, 1, 1);  for(inti:d) {System.out.print (i+"  "); }      }}

Operation Result:

1 2 3 4 8 5 7 6 1  2  3  4  5  false2  3 4 5 1 2 3  4  5  

Note: Some of these methods are valid only for one-dimensional arrays

Related knowledge of arrays

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.