Basic knowledge of Java | Full solution of array Detailed explanation of one-dimensional array


theme: channing-cyan

Keep creating and accelerate growth! This is my 14th day participating in the "Nuggets Daily New Plan·October Update Challenge", click to view event details

array

For ease of use, we can use a new data type-array-for some data that have the same type, a fixed relationship with each other, and a certain order, so that there is no need to define many simple variables. - An array is a collection of ordered data. Each array element in the array has the same array name. You can use a subscript to uniquely determine the array element.

one-dimensional array

Array types can be any data type in the Java language, including simple data types and composite data types. - Definition format of one-dimensional array 数组类型 数组名[]; //或者 数组类型 [] 数组名; - Code example: java int score[]; String coure[]; double [] area; float [] mathScore,enlishScore,peScore Note: There is no space allocated for array elements in the Java array definition, which means that the number of array elements is not specified in brackets [] when declaring the array. Therefore we have to manually allocate storage space for the array. - The format is as follows

java new 数据类型[数组大小]; After allocating space to the array, if the array elements are simple data types, the array is initialized and each array element has a default value. If the array elements are String or composite types, the default value of each array element is null. Therefore we need to assign initial values ​​to non-simple data types. - The format of referencing array elements is: 数组名[整型表达式] Note: Get the number of array elements:数组名.length

  • code example

java public class TestArry{ int a[]=new int[10]; Object b[]=new Object; String c[]=new String; for (int i=0;i<10;i++){ System.out.println(a[i]+""+b[i]+""+c[i]); } }

One-dimensional array initialization

Before using an array, the array needs to be initialized. - After the array is defined and initialized, its array elements correspond one-to-one to the values ​​enclosed in curly braces. For example: java int a[]={1,3,5,7,9}; int b[]=new int[]{2,4,6}; we can output the values ​​​​a[0], a[1], and... of array a according to the subscripts: 1, 3, 5, 7, 9. Array b is dynamically allocated 3 Integer storage space of type int. - Note: When declaring an array, the number of array elements should not be specified. Like int d[10], it is the wrong way to define an array.

Guess you like

Origin blog.csdn.net/y943711797/article/details/132972172