The Java array is detailed

Source: Internet
Author: User
Tags float double

First, the concept of arrays

    • An array is a combination of multiple identical types of data that enables unified management of these data
    • The elements in the array can be any data type, including the base data type and the reference data type
    • The array is a reference type, the array data is an object, and each element in the array is equal to the member variable of that object

Two or one-D arrays

Dynamic initialization: An array declaration that separates the allocation space of an element from the assignment of a value

Static initialization: Allocates space and assigns values to array elements while defining arrays

Note: An array is a reference type, and its elements are equivalent to the member variables of the class, so once the array is allocated space, each element is implicitly initialized in the same way as member variables

Once an array is initialized, its length is immutable.

 PackageCom.yyx.pratice; Public classJavapratice { Public Static voidMain (string[] args) {/** For basic data type byte short int long *: After creating an array, the default value is 0*/        byte[] bytearr=New byte[2]; bytearr[0]=1;  for(byteB:bytearr) {System.out.print (b+" ");                } System.out.println (); /** For the basic data type float double *: The default value is 0.0*/        float[] floatarr=New float[2]; floatarr[0]=2.1f;  for(floatF:floatarr) {System.out.print (f+" ");                } System.out.println (); /** For basic data type char: its default value is a space*/        Char[] chararr=New Char[2]; chararr[0]= ' a ';  for(CharC:chararr) {System.out.print (c+" ");                } System.out.println (); /** For the base data type, Boolean, the default value is false after the array is created*/        Boolean[] booleanarr=New Boolean[2];  for(BooleanC:booleanarr) {System.out.print (c+" ");                } System.out.println (); /** For arrays of reference-type variables: Their default value is null*/string[] Strarr=NewString[2];  for(String Str:strarr) {System.out.print (str+" "); }    }}

Three or two-D arrays

 PackageCom.yyx.pratice; Public classJavapratice { Public Static voidMain (string[] args) {/** Initialization of one-dimensional arrays*/        int[] arr1 = {13, 35, 56 }; int[] arr2 =New int[] {3, 5, 8};//Static Initialization        int[] Arr3 =New int[3]; arr3[0] = 6;//Dynamic InitializationARR3[1] = 9; arr3[2] = 12; /** Two-dimensional array initialization*/string[][] str1= {"Jack", "Tom"}, {"Lucy", "Jerry" } }; String[][] str2=NewString[][] {"Jack", "Tom"}, {"Lucy", "Jerry"}};//Static Initializationstring[][] Str3 =NewString[2][2]; str3[0][0] = "Jack"; str3[0][1] = "Tom"; str3[1][0] = "Lucy"; str3[1][1] = "Jerry";  for(inti = 0; i < str3.length; i++) { //control line Count  for(intj = 0; J < Str3[i].length; J + +) { //control number of columns System.out.print (Str3[i][j]+ " "); }        }    }}

Iv. allocation of arrays in memory

V. Common Exceptions for arrays

    • NULL pointer exception nullpointerexception
    • Index out-of-bounds exception arrayindexoutofboundsexception
 PackageCom.yyx.pratice; Public classJavapratice { Public Static voidMain (string[] args) {/** NULL pointer exception*/        //First Kind        Try {            Boolean[] booleanarr=New Boolean[3]; Booleanarr=NULL; System.out.println (booleanarr[0]); } Catch(Exception e) {e.printstacktrace (); }            //The second Kind        Try{string[] Strarr=NewString[3]; System.out.println (strarr[0].tostring ()); } Catch(Exception e) {e.printstacktrace (); }        //Third Kind        Try {            int[] intarr=New int[3][]; System.out.println (intarr[2][0]); } Catch(Exception e) {e.printstacktrace (); }                /** Index value out of bounds*/        Try {            int[] arr=New int[3]; arr[4]=0; } Catch(Exception e) {e.printstacktrace (); }    }}

Vi. tool classes for manipulating arrays arrays

The Java array is detailed

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.