1: A single variable can store information
2: Used to store data sets with the same data type. You can use a common name to refer to the data stored in the array. .
Features
Arrays can store any type of data, including primitive data types and reference data types, but once the type of the array is specified, it can only be used to store data of the specified type.
Declare an array variable to store the array
Syntax
Data type[] Array name
Data type array name[]
//声明一个int类型 名为 numebr 的数组 int [] number; int number []; //以上两种方法都可以
Create a new array object and assign it to the value created in the previous step Array variables:
1: Use the new operator
int [] number = new [5]; // 意思是把number这个数组的长度设置为5, //但是数组的长度一旦被设置,就无法更改
2: Directly initialize the array
int [] number = {1,2,3,4,5,6} //也可以直接用进行赋值,写入相同类型的数值即可
3: Access or modify the storage in this array Data
a: If you need to access the elements in the array, you need to use the array name [subscript] form to access
b: Note that the maximum subscript of the array is: the length of the array -1 ;The initial subscript is: 0
c: The length of the array can be obtained by using the length attribute of the array. Each array object will have a length attribute to represent the length of the array, that is, the number of array elements.
//输出number数值第2个数字 System.out.println(number[1])
4: Traverse the array:
If you want to output all elements in the array to one side, you can use a for loop
int [] numebr = {1,2,3,4,5} for(int i=0;i<number.length;i++){ System.out.println(number[i]) }
Create Two-dimensional array
There are also two ways to create a two-dimensional array:
Method 1:
Use the new operator
int [][] number = new int[5][5];
Method 2: Declare the array and assign an initial value
int [][] nuber = {{1,2,3,},{4,5,6,},{7,8}}; //二维数组就是在一个大的数组内,还包含了二级数组,可以理解成嵌套的意思 //若要输出二维数组,也可以采用多个for循环的方式 int [][] nuber = {{1,2,3},{4,5,6},{7,8,9}}; for(int i =0;i<nuber.length;i++){ for (int j = 0;j<nuber.length;j++){ System.out.println(nuber[i][j]); } }
Issues to note:
Array out-of-bounds exception error:
Array out-of-bounds is a runtime exception, when the array is accessed When the length exceeds the length of the array itself, an array out-of-bounds exception will be thrown java.lang.ArrayIndexOutOfBoundsException
Array pointing and passing
The essence of reference passing is in stack memory **field (collection)** points to data in heap memory. When an array assignment or an array function parameter is passed, a value reference is passed, that is, the pointer to the data is passed so that it points to the same block of data memory, thereby achieving the purpose of array transfer. Therefore, when data is heaped in memory, other references pointing to the data segment will be modified.
Array subscript out-of-bounds exception:ArrayIndexOutOfBoundsException
String subscript out-of-bounds exception:StringIndexOutOfBoundsException
Class conversion exception:ClassCastException
NullPointerException:NullPointerException
Number formatting exception:NumberFormatException
Input cannot match exception:InputMismatchException
Parse exception:ParseException
No such element exception: NoSuchElementException
File not found exception:FileNotFoundException
Arithmetic exception:ArithmeticException
Wrong parameter exception:IllegalArgumentException
The above is the detailed content of Example analysis of arrays and common exceptions in Java. For more information, please follow other related articles on the PHP Chinese website!