Java basic learning notes (2)

1. Array

1. Declare array

// 声明数组的两种方式
int array[];
int[] array2;// 建议该种写法

// 开辟空间
array = new int[3]; // 开辟3个空间(长度为3的数组)
// 注:开辟空间后,里面保存的是该类型的默认值

double[] array3 = new double[5];// 声明长度为5的double数组

// 声明并赋值的两种方式
int[] array4 = {
    
    1, 2, 3};
int[] array5 = new int[]{
    
    1, 2, 3, 4};

array2 = new int[5];
// 给第一个元素赋值
array2[0] = 5;

2. Precautions

  • The type and length of the array cannot be modified

  • Only values ​​of specified types can be stored in the array.

  • The subscript of the array cannot exceed the maximum length of the array

3. Get the array length

int[] arr = {
    
    1, 2, 3};
int length = arr.length; // 数组长度,结果:3

4. Array tool class Arrays

Arrays is java.utila tool class used to operate arrays. Through this tool class, we can easily complete some operations on arrays. Here are some methods that may be used:

a. equals

Determine whether all elements of two arrays are equal when they are not null

int[] arr = {
    
    1, 2, 3};
int[] arr2 = {
    
    1, 2, 3};
boolean equals = Arrays.equals(arr, arr2); // 判断arr与arr2是否相等,结果:true

b. sort

Sort array

int[] arr = {
    
    1, 3, 2};
Arrays.sort(arr); // 对arr进行排序,结果:[1, 2, 3]

c. toString

Concatenate arrays into strings

int[] arr = {
    
    1, 2, 3};
System.out.println(Arrays.toString(arr)); // 将arr数组拼接成字符串,结果:[1, 2, 3]

**Note:** If you output an array directly, the toString method of the array will be called by default. The default toString method of the array is as follows, so you may get [I@1be6f5c3results similar to

public String toString() {
    
    
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

d. copyOf

Copy the data of the specified length from the original array, and fill the excess with the default value of the data type

int[] arr = new int[]{
    
    1, 2, 3, 4};
int[] arr1 = Arrays.copyOf(arr, 3); // 从arr中复制3个元素,赋值给arr1
System.out.println(Arrays.toString(arr1)); // 结果:[1, 2, 3]

e. fill

Assign all elements in the array to the specified value

int[] arr = new int[3];
Arrays.fill(arr, 5); // 将arr中所有元素赋值为5
System.out.println(Arrays.toString(arr)); // 结果:[5, 5, 5]

5. Array copy

int[] arr = {
    
    1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int[] arr2 = new int[8];
/*
 从一个数组中复制到另一个数组指定位置、长度的数据
 arr是初始数组,
 0代表从下表为零出开始复制,
 arr2是目标数组,
 3代表复制到目标数组中的位置
 4代表将要复制的数据长度(不能超出初始数组大小)
*/
System.arraycopy(arr, 0, arr2, 3, 4);
System.out.println(Arrays.toString(arr2)); // 结果:[0, 0, 0, 1, 2, 3, 4, 0]

6. Two-dimensional array

A two-dimensional array is that each element in the array is an array, that is, an array nested array

// 二维数组中有3个子元素(数组),每个子元素的长度为4
int[][] arr = new int[3][4];
for (int i = 0; i < arr.length; i++) {
    
    
    for (int j = 0; j < arr[i].length; j++) {
    
    
        // 随机生成0-30的数字,放到数组中
        arr[i][j] = (int) (Math.random() * 30);
    }
    System.out.println(Arrays.toString(arr[i]));
}
System.out.println(Arrays.toString(arr));
/* 
结果:
[12, 10, 0, 1]
[18, 17, 8, 29]
[3, 17, 12, 25]
[[I@6b884d57, [I@38af3868, [I@77459877]
*/

2. Collection

There are many kinds of collections in Java. Below we only explain the two commonly used collections (List and Map).

1. List

List is an ordered collection. We can create a List collection by using the following statement

List list = new ArrayList();

However, when we create a List, we often need to specify the generic type of the List (that is, the data saved in the specified List can only be of the specified type). If the String generic type is specified as follows, then only String type data can be saved in the List.

List<String> list = new ArrayList<>();

Below we introduce the more commonly used List methods

a. add

Through add we can add data to the List

// 直接添加数据是追加在List集合末尾
list.add("数据"); // 结果:[数据]
// 通过下标添加数据,将数据添加到指定位置
list.add(0, "插入数据"); // 结果:[插入数据, 数据]

b. size

Get the number of elements in the List collection

list.size(); // 结果:2

c. contains

Determine whether the List collection contains an element

list.contains("数据"); // 结果:true

d. get

Get the data in the List collection by subscript

list.get(1); // 结果:数据

e. clear

Clear the current List collection

list.clear(); // 结果:[]

f. addAll

Add all elements of another List collection to the current List collection

List<String> list2 = new ArrayList<>();
list2.add("数据1");
list2.add("数据2");
list2.add("数据3");
list.addAll(list2); // 结果:[数据1, 数据2, 数据3]

g. remove

Remove elements from List collection

// 通过数据移除
list.remove("数据1"); // 结果:[数据2, 数据3]
// 通过下标移除
list.remove(1); // 结果:[数据2]

h. removeAll

Remove all the same elements from another List collection from the current List collection

list.removeAll(list2); // 结果:[]

2. Map

The data in the Map is stored in the form of key-value pairs. We can create a Map in the following way

Map map = new HashMap();

However, it is often necessary to specify the generic type. As follows, we specify that the key and value of the Map must be of type String

Map<String, String> map = new HashMap<>();

Below we introduce the more commonly used Map methods

a. put

Through put we can add key-value pairs to the Map

map.put("数据1", "数据1内容"); // 结果:{数据1=数据1内容}

**Note:** If you add content with the same key to the Map, the latter will overwrite the former

map.put("数据1", "数据1修改内容"); // 结果:{数据1=数据1修改内容}

b. size

Used to get the number of key-value pairs in the Map

map.size(); // 结果:1

c. get

Get the value corresponding to the specified key in the Map

map.get("数据1"); // 结果:数据1修改内容

d. clear

Clear Map

map.clear(); // 结果:{}

It is. putAll

Add all key-value pairs in another Map to the current Map

Map<String, String> map2 = new HashMap<>();
map2.put("数据1", "数据1内容");
map2.put("数据2", "数据2内容");
map.putAll(map2); // 结果:{数据1=数据1内容, 数据2=数据2内容}

f. remove

Delete specified element

map.remove("数据1"); // 结果:

1. Conditional judgment

1. if

Conditional statements are almost universal in programming languages, but in Java, if there is only one judgment execution statement, you can omit the square brackets (but sometimes this writing method is not recommended for the readability of the code)

int a = 3;
// if语句判断
if (a > 4) {
    
    
    System.out.println("a小于等于4");
}
// if...else...语句判断
if (a < 5) {
    
    
    System.out.println("a小于5");
} else {
    
    
    System.out.println("a大于等于5");
}
// if...else if...else语句判断
int age = 23;
if (age >= 0 && age <= 8) {
    
    
    System.out.println("儿童");
} else if (age > 8 && age <= 12) {
    
    
    System.out.println("少年");
} else if (age > 12 && age <= 35) {
    
    
    System.out.println("青年");
} else if (age > 35 && age <= 60) {
    
    
    System.out.println("中年");
} else {
    
    
    System.out.println("老年");
}
// 省略中括号
int age = 23;
if (age >= 0 && age <= 8)
    System.out.println("儿童");
else if (age > 8 && age <= 12)
    System.out.println("少年");
else if (age > 12 && age <= 35)
    System.out.println("青年");
else if (age > 35 && age <= 60)
    System.out.println("中年");
else
    System.out.println("老年");

2. switch

In Java, switch can match byte, short, int, char, String, and enumeration types. However, if the switch is of char type and the case value is int, it will match ASCII.

int a = 2;
switch (a) {
    
    
    case 1:
        // 当a == 1时从该行开始运行
        System.out.println(1);
        break; // 结束所在的代码块儿,即switch
    case 2:
        System.out.println(2);
        break;
    default:
        // 当其他项都不匹配时运行的代码,default可以省略
        System.out.println("您输入的值不合法!");
        break;
}

// 多行匹配
switch (a) {
    
    
        // 输入1或2都是输出去a班
    case 1:
    case 2:
        System.out.println("你输入的班级是" + a + ",要去a班");
        break;
        // 输入3或4都是输出去b班
    case 3:
    case 4:
        System.out.println("你输入的班级是" + a + ",要去b班");
        break;
        // 输入其他值去c班
    default:
        System.out.println("您属于其他班,去c班");
        break;
}

Guess you like

Origin blog.csdn.net/jl15988/article/details/121475313