Java Array Simple usage

Source: Internet
Author: User

An array is a set of data with the same data type, Java supports multiple arrays, each basic unit of a one-dimensional array is data of the basic data type, and the two-dimensional array is the one-dimensional array of one-dimensional arrays, and so on, Each base cell of an n-dimensional array is a n-1-dimensional array of n-1 arrays. The following example illustrates the use of a Java array with a one-dimensional array.

1. Array declaration

There are two types of array declarations (the positions of brackets are different):

int arr[];int[] arr2;
2, array initialization

There are two forms of array initialization, as follows (using new or not using new):

intnewint[]{13579};int[] arr2 = {246810};
3. Iterating through an array

The traversal array is available For/foreach, as follows:

     Public Static void Main(string[] args) {intArr[] =New int[]{1,3,5,7,9};int[] arr2 = {2,4,6,8,Ten}; for(inti =0; i < arr.length; ++i) {System. out. print (Arr[i] +"\ T");//1 3 5 7 9} for(intX:ARR2) {System. out. print (x +"\ T");//2 4 6 8}    }
4. Arrays.fill () padding array

Using the static method of the Arrays class requires the import package Java.util.Arrays, which defines a number of overloaded methods.

void Fill (int[] A, int val) fills all
void Fill (int[] A, int fromIndex, int toindex, int val) fills the element of the specified index

        int[] Arr3 =New int[5]; for(intX:ARR3) {System. out. print (x +"\ T");//0 0 0 0 0 all initialized to 0} System. out. println (); Arrays.fill (ARR3,Ten); for(intX:ARR3) {System. out. print (x +"\ T");//10 10 10 10 10 all filled to ten} System. out. println (); Arrays.fill (ARR3,1,3,8); for(intX:ARR3) {System. out. print (x +"\ T");//10 8 8 10 10 Fill the specified index} System. out. println ();
5, Arrays.sort () array sorting

void sort (int[] a) all sorts
void sort (int[] A, int fromIndex, int toindex) Sorts the elements of the specified index

        int[] Arr4 = {3,7,2,1,9}; Arrays.sort (ARR4); for(intX:ARR4) {System. out. print (x +"\ T");//1 2 3 7 9} System. out. println ();int[] Arr5 = {3,7,2,1,9}; Arrays.sort (ARR5,1,3); for(intX:ARR5) {System. out. print (x +"\ T");//3 2 7 1 9} System. out. println ();
6, arrays.copyof () copy array

Int[] CopyOf (int[] original, int newlength) to copy an array, specifying the new array length
Int[] Copyofrange (int[] original, int from, int. to) copy an array, specifying the index of the original array being copied

        int[] Arr6 = {1,2,3,4,5};int[] arr7 = arrays.copyof (ARR6,5);//1 2 3 4 5        int[] Arr8 = Arrays.copyofrange (ARR6,1,3);//2 3         for(intX:ARR7) {System. out. print (x +"\ T"); } System. out. println (); for(intX:ARR8) {System. out. print (x +"\ T"); } System. out. println ();

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Array Simple usage

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.