Array
Number leader in Java like this (define array)
int [] arr; // Recommended int arr[];
first, the definitionwhat is an array?
An array is the same type of an object sequence or primitive type data sequence that is encapsulated together with an identifier name. a simple set of elements of the same type .
Why use arrays?
When you need to store a large amount of data, such as the need to read 100 numbers, then you need to define 100 variables, and the array solves this problem, through the array container to the same type of elements with an identifier, easy to store and manipulate.
Second, format
Format 1: element type [] Array name = new element type [element number or array length]
Such as:
int New int [10];
Format 2: element type [] Array name = new element type []{element, element, element,.....}
Such as:
int New int []{1,2,3,4}; int [] arr = {1,2,3,4}; // special initialization, storage space allocation (equivalent to using new) is the responsibility of the compiler.
Iii. Advantages and disadvantages
Advantages:
1. Query elements faster by index
2, can store a large amount of data
3. It is convenient to iterate through the array by index
Disadvantages:
1, according to the content to find the element speed slow
2. Once the size of the array is determined, it cannot be changed.
3, arrays can only store one type of data
4. Efficiency of adding and removing elements is slow
5. No method is encapsulated and all operations need to be defined by the user.
Iv. Common Anomalies
Null pointer exception (NULLPOINTEREXCEPTION)
Typically, you create only the reference to an array without pointing to any objects, and are used by other objects or methods. such as:
Static int [] arr; Public Static void Main (string[] args) { for (int i = 0; i < arr.length; i++) { System.out.print ln (arr[i]);} }
Array out of bounds (arrayindexoutofboundsexception)
The index value of the array starts at 0 and accesses the non-existent index value. such as:
Static int [] arr = {1, 2, 3 }; Public Static void Main (string[] args) { System.out.println (arr[-1]); System.out.println (arr[3]);}
Java Array Learning Notes