一、Arrays工具类
1. 常用方法
(1)Arrays.toString(array)
- 功能: 打印
- 返回: 字符串
- 传入数组,返回拼接数组值后的字符串
(2)Arrays.equals(array1, array2)
- 功能: 比较两个数组会不会相等
- 返回: 布尔值
(3)Arrays.sort(array)
- 功能: 排序
(4)Arrays.copyOf(array, newLength)
- 功能: 复制
- 返回: 新数组
- 传入原始数组array和长度newLength,这样拷贝就从原数组中拷贝多少个长度的数组值
(5)代码演示
package SecondArrays;
import java.util.Arrays;
public class SecondApp {
public static void main(String[] args) {
int[] app = {1, 2, 6, 54, 65, 73};
System.out.println(app);
System.out.println(Arrays.toString(app));
int[] app2 = {1, 2, 6, 54, 65, 73};
System.out.println("== : "+ (app == app2));
System.out.println("Arrays.equals : "+ Arrays.equals(app, app2));
int[] app3 = {1, 2, 54, 65, 73};
System.out.println("Arrays.equals app app3 : "+ Arrays.equals(app, app3));
int[] app4 = {78, 63, 99, 56, 79, 1, 23, 33, 63};
System.out.println("原数组" + Arrays.toString(app4));
Arrays.sort(app4);
System.out.println("排序后的数组" + Arrays.toString(app4));
int[] app5 = Arrays.copyOf(app4, 5);
int[] app6 = Arrays.copyOf(app4, 15);
System.out.println(Arrays.toString(app5));
System.out.println(Arrays.toString(app6));
int[] nums = {5, 10, 23, 6, 36, 69};
nums = Arrays.copyOf(nums, nums.length + 1);
nums[nums.length - 1] = (int) (Math.random() * 16 + 1);
System.out.println(Arrays.toString(nums));
}
}
|