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 number of rows for(intj = 0; J < Str3[i].length; J + +) {//number of control columnsSystem.out.print (Str3[i][j] + ""); } } }}
Iv. allocation of arrays in memory
1. Memory structure of one-dimensional arrays
2. Memory structure of two-D arrays
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
arrays commonly used methods have sort (), toString (), and other methods to view the class library
Package Com.yyx.pratice; Import java.util.Arrays; Public class Javapratice { publicstaticvoid main (string[] args) { int New int [] {2, 6, 3, 5, 9 }; Arrays.sort (arr); // Array Sorting System.out.println (arrays.tostring (arr));} }
The Java array is detailed