Declaration, initialization and use of one-dimensional array in java

Declaration, initialization and use of one-dimensional array in java

Arrays can store multiple elements of the same type, where the type can be any of the java data types

1. Two ways to declare a one-dimensional array

Mode 1: Array element type [ ] Array name;
Mode 2: Array element type Array name [ ];

	//声明一个存储分数的数组
	//方式1
	int[] scoreArray1;
	//方式2
	int scoreArray2[];
	//也可以声明其他类型的数组
	char[] sex1;
	char sex2[];
	boolean[] isRoll1;
	boolean isRoll2[];
	String[] department1;
	String department2[];
	byte[] age1;
	byte age2[];
		

Note: Cannot be used without an initialized array. (can be initialized to null)

	//未初始化使用数组
	int[] scoreArray;
	System.out.println(scoreArray);

An error will be reported at runtime, as shown in the following figure:
report an error
Remedy: Initialize the array by assigning a value. If you do not intend to assign a value, you can assign a null value.

	//给数组赋null值
	int[] scoreArray = null;
	System.out.println(scoreArray);

insert image description here
2. Initialization of one-dimensional arrays

The initialization of the array can be initialized directly when the array is declared, or it can be initialized after the declaration.

Mode 1: array element type [ ] array name = new array element type [array length]; ------------ Default initialization value: int type is 0, double type is 0.0, boolean type is false , String type is null, char type is the character corresponding to 0, and float type is 0.0.

Mode 2: Array element type [ ] Array name = new Array element type [] {element 1, element 2, ...}; ------------ The length of the array is determined according to the number of elements

Method 3: Array element type [ ] array name = {element 1, element 2, ...}; ------------ Simplified version of method 2, the length of the array is determined according to the number of elements

	//方式1
	int[] age = new int[50];//默认初始化的值为0
	//方式2
	double[] scoreArray = new double[]{98, 87.5};//scoreArray的长度为2
	//方式3
	String[] department = {"INFOR", "COMPU", "COMMU", "ENGLISH"};//department的长度为4

Note: The length of the array must be determined when the array is initialized

3. How to use one-dimensional arrays

<1> Access and modify the data in a one-dimensional array
Access the array according to the array index or subscript. The range of the abbreviated print is 0 to the array length -1. If the index range is exceeded, an error will be reported at runtime. When modifying the data in the array, you only need to select the data to be modified according to the index and assign a new value.

	//访问scoreArray下标为0的数据
	double d =  scoreArray[0];
	System.out.println(d);
	//修改scoreArray下标为0的数据为88.7
	scoreArray[0] = 88.7;
	System.out.println(scoreArray[0]);

Revise
<2> Get the length of a one-dimensional array The length of
all one-dimensional arrays can be obtained through the array name.length .

	//获取department数组的长度
	String[] department = {"INFOR", "COMPU", "COMMU", "ENGLISH"};
	int l = department.length; 
	System.out.println(l);

length
<3> Assignment between one-dimensional arrays

	int[] a = new int[10];
	int[] b = {3, 5, 6, 2, 45, 28};
	System.out.println(a[0] + " " + b[0]);
	a[0] = b[0];//将b[0]的值赋给a[0]
	System.out.println(a[0] + " " + b[0]);
	b[4] = a[4];//将a[4]的值赋给b[4]
	System.out.println(a[4] + " " + b[4]);

mutual assignment
<4> Filling of one-dimensional arrays
Arrays.fill(array name, value); ------------Use the specified value to fill the specified array.
When using this method, you need to import java.util.Arrays Bag.

	int[] b = new int[3];
	Arrays.fill(b, 100);
	System.out.println(b[0]);
	System.out.println(b[1]);
	System.out.println(b[2]);

filling
<5> Traversal of one-dimensional arrays

	//输出数组中的元素
	int[] a = new int[]{67, 778, 45, 53, 80};
	for(int i = 0; i < a.length; i++){
		System.out.print(a[i] + " ");
	}

traverse
<6> The most value problem of one-dimensional arrays

Idea: Determine the first element in the array as the maximum value (minimum value), store it in a temporary variable, and then use a for loop to compare the temporary variable with other elements in the array one by one. If the array element is greater than (less than) the temporary variable The value of the temporary variable is updated until the entire array is traversed, and the last value stored in the temporary variable is the maximum value (minimum value).

	//一维数组的最值问题(此处以最大值为例,最小值同理)
	int[] a = {7, 35, 45, 2, 67, 889} ;
	int max = a[0];
	for(int i = 1; i < a.length; i++){
		if(a[i] > max)
			max = a[i];
	}
	System.out.println(max);

maximum value

There are many other uses of one-dimensional arrays, which are not listed here.

The above are relatively basic knowledge of one-dimensional arrays, which need to be mastered.

PS: If the above content is wrong, please point it out! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324225655&siteId=291194637