Java entry tutorial: Chapter 5 array

zhaozj2021-02-16  79

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 INTARRAY [J]) {Int T = Intarray [I]; INTARRAY [i] = INTARRAY [J]; INTARRAY [J] = T;} for (i = 0; i Java Bubblesort-9 1 25 30 70] @@ § 5.2 Multi-dimensional arrays, like C, C , and multi-dimensional array in Java is considered an array of arrays. For example, a two-dimensional array is a special one-dimensional array, and each element is another one-dimensional array. Let's take a two-dimensional array as an example to explain, high dimensional situation is similar. First, the definition of the two-dimensional array is: Type ArrayName [] []; for example: int ida [] []; like a one-dimensional array, then the array element does not assign memory space, the same Use operator NEW to allocate memory before accessing each element. For high-dimensional arrays, allocate memory spaces have the following methods: 1 Directly allocate space for each dimension, such as int a [] [] = new int =, from the highest dimension, Assign space for each dimension, such as int a [] [] = new int [2] []; a [0] = new int [3]; a [1] = new int [3]; completion 1 Function. This is different from C, C , and must refer to the length of each dimension once in C, C . Second, the reference to the two-dimensional array element for each element in the two-dimensional array, the reference method is: arrayname [index1] [index2] where index1, index2 is subscript, can be integer or expressions, such as A [2] [3] Wait. Similarly, the subscript of each dimension is from 0. Third, the initialization of the two - dimensional array has two formulas: 1 is directly assigned to each element. 2 Initialization in the same time of the definition array. Such as: int a [] [] = {{2, 3}, {1, 5}, {3, 4}}; define a 3 × 2 array and assigns each element.

转载请注明原文地址:https://www.9cbs.com/read-14015.html

New Post(0)