Java two-dimensional array and dynamic array ArrayList class

Java two-dimensional array

The array provided in the Java language is used to store elements of the same type of fixed size.

1. Two-dimensional array initialization and declaration. The declaration of
array variables and the creation of arrays can be completed with one statement, as shown below:

		int a[][] = new int[2][3];
		int[][] arr = {
    
    {
    
    1,2,3},{
    
    4,5,6},{
    
    7,8,9}};

2. Two-dimensional array traversal

//遍历二维数组
public class Traverse_a_two_dimensional_array {
    
    
	public static void main(String[] args) {
    
    
		int[][] arr = {
    
    {
    
    1,2,3},{
    
    4,5,6},{
    
    7,8,9}};//静态创建
		
		//遍历数组的第一个方法
		for(int i = 0;i < arr.length;i++){
    
       
			//遍历arr[0],arr中元素第一个数组
			for(int j = 0;j < arr[i].length;j++){
    
    
				System.out.print(arr[i][j]);
			}
		}		
	}
}


public class Traverse_a_two_dimensional_array {
    
    
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int[][] arr = {
    
    {
    
    1,2,3},{
    
    4,5,6},{
    
    7,8,9}};//静态创建

		//遍历数组的第二个方法
		for (int[] is : arr) {
    
    
			for (int i : is) {
    
    
				System.out.print(i);
			}
		}		
	}
}

3. Arrays class (not used yet)
java.util.Arrays class can easily manipulate arrays, all the methods it provides are static.
It has the following functions:
Assign value to the array: through the fill method.
Sort the array: through the sort method, in ascending order.
Compare arrays: compare whether the element values ​​in the arrays are equal through the equals method.
Find array elements: Binary Search can be performed on the sorted array through the binarySearch method.

ArrayList class

The ArrayList class is an array that can be dynamically modified. The difference from an ordinary array is that it has no fixed size limit, and we can add or delete elements. ArrayList inherits AbstractList and implements the List interface.

initialization:

		import java.util.ArrayList; // 引入 ArrayList 类
        ArrayList<String> sites = new ArrayList<>(); // 创建一个动态数组

Add element:
add element to ArrayList can use add() method:

		sites.add("Runoob");

Delete elements: delete elements in
ArrayList can use the remove() method:

 		sites.remove(3); // 删除第四个元素

Calculating size: To
calculate the number of elements in ArrayList, you can use the size() method:

		 System.out.println(sites.size());

ArrayList sorting: The
Collections class is also a very useful class, located in the java.util package. The sort() method provided can sort a list of characters or numbers.
The sort() method sorts the elements in the dynamic array according to the specified order.
The syntax of the sort() method is:
Note: ArrayList is an object of ArrayList class.
arraylist.sort(Comparator c) comparator-order method

		import java.util.Comparator;
        // 降序
        sites.sort(Comparator.reverseOrder());

Guess you like

Origin blog.csdn.net/weixin_46801232/article/details/108655665