I. Array concepts
I. Definition of arrays
An array is a computer memory opened up in theContinuous storage spaceThis is used for storing a set ofSame type of dataof the container.
II. Declaration of arrays + length of arrays
When defining an array, you need to determine the length of the array (the number of elements), which cannot be changed after the determination;
Get the length of the array: array.length
1. Format 1 (default initial values)
-
datatype [ ] array name = new datatype [ number of arrays ]
Example: int [ ] money = new int [ 100 ];
Length of array: (100).
2. Format 2 (assigning initial values)
-
datatype [ ] array name = new datatype [ ]
Example: int [ ] money = new int [ ]{100,200,300,.... .n};
Length of array: (n).
3. Format 3 (abbreviated format 2)
- Data type [ ] array name =
Example: int [ ] money = {100,200,300};
Length of array: (3).
III. Access to arrays
Example:
int [ ] money = new int [ 100 ]; //declare an array of 100 elements
Access to the first element (array subscript index from 0)
money[ 0 ] = 100;
Accessing the second element
money [ 1 ] =200;
...
Access to the 100th element
money [ 99 ] = 1000;
Accessing the 101st element (an array out-of-bounds exception, an array out-of-bounds does not show a compile error, but a runtime error)
money[ 100 ]= 1100;
IV. Iteration of arrays
Example:
int [] arr = new int [6];
for(index = 0;index < ;index++){
(arr[index])
}
V. Characteristics of arrays
1. Once the array length is specified, it cannot be changed;
2. Once the type of the array is declared, the array can only hold arrays of data of the same type;
3. The array type can be any data type, including basic types and reference types;
Arrays are indexed: they start at zero and end at array.length-1.
VI. Disadvantages of arrays
1. Once the length of the array is specified, it cannot be changed;
2. Deleting and adding elements is inefficient;
3. There is no way to get the actual number of elements in the array, there is no corresponding method or property to get it;
VII. Examples
/**
* @author yeye
* @desc Array access example
* @date 2024
*/
public class ArrayVisitDemo {
public static void main(String[] args) {
// arr [5] = 6; // Array out of bounds, doesn't show compilation error, but runtime error
("The length of the array is: "+); // Array traversal.
// array traversal
for(int index = 0;index <;index++){
(arr[index]);
}
("The elements of the array are: "+arr[0]+" "+arr[1]+" "+arr[2]+" "+arr[3]+" "+arr[4]););
}
}
II. One-dimensional and two-dimensional arrays
I. Definition of a one-dimensional array
Each element of an array carries only one subscript, and such an array is called a one-dimensional array. Before using Java arrays, you should declare the array and allocate storage space for the array.
II. Declaring one-dimensional arrays
1. type identifier [ ] array name = null; Example:
int [ ] list = null; //declare list as an array of one-dimensional type
2. type identifier Array name [ ] = null; Example:
int list [ ] = null; //declare list as an array of one-dimensional type
III. Initializing one-dimensional arrays
1. Initialize the array by specifying the initial value directly, Example:
int [ ] list = {1,2,3};
2. Initialization using the new keyword
Initializing an array with the new keyword not only allocates the storage space needed for the array, but also assigns values to the array elements according to Java's default initialization principles.
IV. Two-dimensional arrays
Example: int money [ ] = new int [3] [12];
Iteration over 2D arrays
int money [ ] = new int [3] [12];
for(int row = 0;row <3;++row){
for(int column = 0;column<13;++column){
money[row][column] =100;
}
}
III. Algorithms for arrays (bubble sort method)
Catchphrase :
Bubble sort to know; inside and outside the loop two sets; outer ring array traversed to; inner ring boundaries calculated; left and right elements to determine the size of; temporary variable exchange good.
// outer loop traverses the entire array (the last array does not need to be traversed)
for(i = 0;i < (array length-1);i++){
//Memory loop control element exchange that is bubbling, generally for the length of the array - outer loop traversal -1
for(j = 0;j <(array length-i-1);j++){
// ascending order - if the front is greater than the back then swap; descending order - if the back is greater than the front then swap
if(array[j] > array[j+1]){
// introduce temp variable as swap medium
int temp = array[j+1]; //temporary variable
array[j+1] = array[j];
array[j] = temp;
}
}
}
Example:
package array;
/**
* @author yeye
* @desc bubbling sort
* @date 2024
*/
public class BubbleSortDemo {
public static void main(String[] args) {
// Defining arrays
int[] arr =new int [] {3,2,5,8,6,1,4,7};
//bubbling sort
for(int i = 0;i < ;i++){
for(int j =0;j <-i-1;j++){
if(arr[j] > arr[j+1] ){
int temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
// Output the sorted array
for(int i = 0;i < ;i++){
(arr[i]);
}
}
}