# Use of one dimensional array

Keywords: Attribute Java

### Use of one dimensional array

① Declaration and use of one dimensional array

How to declare a one-dimensional array:

```typevar [] or type [] var；
```

For example:

```inta[];
int[]a1;
doubleb[];
String[]c;//Reference type variable array
```

When declaring an array in Java language, you cannot specify its length (the number of elements in the array), for example: inta; / / illegal

```//Dynamic initialization: array declaration and space allocation for array elements are performed separately from assignment
int[]arr= newint;
arr = 3;
arr = 9;
arr =8;

Stringnames[];
names=newString;
names="Tsien Hsueshen;
names="Deng Jia Xian;
names= "Yuan Longping;

//Static initialization: allocate space and assign values to array elements while defining arrays.
int arr[]= newint[]{ 3, 9, 8};or
int[]arr= {3,9,8};

String names[] = {"Si Guang Li","Mao Yisheng","Hua Luogengbest-known Chinese mathematician"}

```

② How to call the elements in the specified position of an array

After defining and allocating space for it with operator new, each element in the array can be referenced;
Reference method of array element: array name [array element subscript]
Array element subscripts can be integer constants or integer expressions. For example, a , B [i], C [6 * I];
The subscript of array element starts from 0; the value range of legal subscript of array with length n: 0 - > n-1;
For example, inta[]=newint; the array elements that can be referenced are a, a, a
Each array has an attribute length indicating its length, for example: a.length indicating the length of array a (number of elements)
Once an array is initialized, its length is immutable

③ How to get the length of an array

④ How to traverse an array

⑤ Default initializers for array elements • ``` >Array element is integer: 0
```
• ``` >Array element is floating point: 0.0
```
• ``` >Array element is char type: 0 or '\ u0000', not '0'
```
• ``` >Array element is boolean type: false
```
• ``` >Array element is reference data type: null (note different from 0! )
```

⑥ Memory parsing of arrays

① To ④ description

```public class ArrayTest {

public static void main(String[] args) {

//1. Declaration and initialization of one dimensional array
int num;//statement
num = 10;//Initialization = assignment
int id = 1001;//Declaration + initialization is the same as the above two lines

//Static initialization: the initialization of array and the assignment of array elements are carried out at the same time
int[] ids;//Declaration here [] indicates array int indicates element type
ids = new int[]{1001,1002,1003,1004};//The different elements between arrays are separated by commas

//Dynamic initialization: the initialization of array and the assignment of array elements are carried out separately
String[] names = new String;

//The same points of static initialization and dynamic initialization: first declare the array on the left, and then initialize (assign) the array on the right.
//The difference between static initialization and dynamic initialization: static initialization indicates what the element is, while dynamic initialization does not indicate what the element is.

//Here is the wrong way to write:
//		int[] arr1 = new int[];
//		int arr2 = new int;
//		int[] arr3 = new int{1,2,3};

//It's also the right way to write:
int[] arr4 = {1,2,3,4,5};//Type inference

//Summary: once the initialization of an array is completed, we will know its length, and the length will be determined.

//2. How to call the element at the specified position of the array: called by the way of angle sign (index, subscript).
//The subscript (or index) of an array starts at 0 and ends at - 1.
names = "Xiao Ming";//Starting from 0
names = "Xiao Wang";
names = "petty thief";
names = "Lao Wang";
names = "Xiao Bai";
//names = "weekly; error will be reported and cannot be run, so it should end at array length - 1.

//3. How to get the length of an array.
//Attribute: length
System.out.println(names.length);//5
System.out.println(ids.length);//4

//4. How to traverse an array
//Method 1
/*System.out.println(names);
System.out.println(names);
System.out.println(names);
System.out.println(names);
System.out.println(names);*/

//Method two
for(int i = 0;i < names.length;i++){//i for index
System.out.println(names[i]);

}
}
}

```

⑤ Default initializers for array elements • ``` >Array element is integer: 0
```
• ``` >Array element is floating point: 0.0
```
• ``` >Array element is char type: 0 or '\ u0000', not '0'
```
• ``` >Array element is boolean type: false
```
• ``` >Array element is reference data type: null (note different from 0! )
```
```public class ArrayTest {

public static void main(String[] args) {
//5. Default initialization value of array element
int[] arr = new int;
for(int i = 0;i < arr.length;i++){
System.out.println(arr[i]);
}//The output is 0. Although they are not defined, the system memory space is automatically assigned to 0 when the array is created
System.out.println("**********");//Separator

short[] arr1 = new short;//The data types before and after should be consistent
for(int i = 0;i < arr1.length;i++){
System.out.println(arr1[i]);
}
System.out.println("**********");//Separator
float[] arr2 = new float;
for(int i = 0;i < arr2.length;i++){
System.out.println(arr2[i]);
}

System.out.println("**********");//Separator
char[] arr3 = new char;
for(int i = 0;i < arr3.length;i++){
System.out.println("----" + arr3[i] + "****");//Output as ----- * * indicates space
}

if(arr3 == 0){//This is to check whether the output of char is 0
System.out.println("Hello!");
}

System.out.println("**********");//Separator
boolean[] arr4 = new boolean;
System.out.println(arr4);//Null output means null value

System.out.println("**********");//Separator
String[] arr5 = new String;
System.out.println(arr5);
if(arr5 == null){//Here is to verify whether the output of boolean is null
System.out.println("Good weather in Beijing!");
}
}

}
```

⑥ Memory parsing of arrays

Simplified structure of memory Memory analysis of one dimensional array There are some small defects here, but they do not affect the general. (Zhang Xueyou and Liu Dehua here should be in the constant pool, not in the heap.)  Published 21 original articles, praised 0 and visited 253

Posted by DigitalDesign on Fri, 07 Feb 2020 04:14:30 -0800