Array is a combination of order data. Each element in an array has the same data type, which can only determine the elements in the array with a uniform array name and subscript. An array has a one-dimensional array and a multi-dimensional array, and we are in part. § 5.1 One-dimensional array 1, one-dimensional array of define one-dimensional array definition of one-dimensional array is: type arrayname []; Type (Type) can be any data type in Java, Including simple types and combinations (see 2.1), array name ArrayName is a legitimate identifier, [] indicates that the variable is an array type variable. For example, int ITARRAY []; sounds a integer array, each element in the array is integer data. Unlike C, C , Java does not assign memory in array elements in the definition of arrays, so [] does not need to indicate the number of elements in the array, that is, the array length, and any array as defined above cannot access it. Element. We must distribute memory spaces, which should be used to use the operator New, which is equipped with: ArrayName = New Type [arraysize]; ARRAYSIZE indicates the length of the array. Such as: INTARRAY = New Int [3]; a memory space occupied by 3 INT type integers is divided into an integer array. Typically, these two parts can be combined, the format is as follows: Type ArrayName = new type [arraysize]; for example: int idarent = new int [3]; two, one-dimensional array elements define an array, and operate NEW Once the memory space is assigned, each element in the array can be referenced. The reference formula of the array element is: ArrayName [index]: index is an array subscript, which can be an integer constant or a table. Such as A [3], B [I] (i is integer), C [6 * I], etc. The subscript is started from 0, and one until the length of the array is 1. For the in-tarray array in the upper case, it has three elements, part of: Intarray [0], Intarray [1], INTARRAY [2]. Note: No Intarray [3]. In addition, with C, C is different, Java is to check the array elements to protect the fullness. At the same time, there is a attribute Length to each array indicate its length, such as: INTARRAY.LENGTH refers to the length of the array INTARRAY.
Example 5.1 Public class arraytest {public static void main (string args []) {INT i; int a [] = new int =; for (i = 0; i <5; i ) a [i] = i; For (i = a.length-1; i> = 0; I -) System.out.Println ("a [" i "] =" a [i]);}} The results are as follows: C: /> Java arraytest a [4] = 4 a [3] = 3 a [2] = 2 a [1] = 1 a [0] = 0 This program assigns each element in the array and then outputs in reverse sequence. Third, the initialization of one-dimensional array to array elements can be assigned according to the examples described above. It can also be initialized at the same time of the definition array. For example, int a [] = {1, 2, 3, 4, 5}; with a comma (,) divided by the individual elements of the array, the system automatically assigns a set of spaces. Different from C, Java does not ask the array as static. Fourth, one-dimensional array procedure example: Example 5.2 Fibonacci Digital Fibonacci Digital Column is defined as: F1 = F2 = 1, Fn = FN-1 FN-2 (n> = 3) Public class fibonacci {public static void main (String Args []) {INT i; int f [] = new int [10]; f [0] = f [1] = 1; for (i = 2; i <10; i ) f [i] = f [i -1] f [i-2]; for (i = 1; i <= 10; i ) system.out.println ("f [f [" i "] =" f [i-1]);} } The result is: C: /> Java Fibonacci F [1] = 1 f [2] = 1 f [3] = 2 f [4] = 3 f [5] = 5 f [6] = 8 f [7 ] = 13 f [8] = 21 f [9] = 34 f [10] = 55 Example 5.3 Bubbling Law Sort (from a small to large) bubbling method Sort the adjacent two elements, and put small Elements swap to the front.
Public class bubblesort {public static void main (string args []) {INT I, J; INT INTARRAY [] = {30, 1, -9, 70, 25}; int L = Intarray.Length; for (i = 0 i