- import java.util.Arrays
- int[] arr = {1,2,3,4,5,6};
- String newArr = Arrays.toString(arr);
- System.out.println(newArr);
- // 执行结果
- [1, 2, 3, 4, 5, 6]
数组拷贝
代码实例:
- import java.util.Arrays;
- public static void func(){
- // newArr和arr引用的是同一个数组
- // 因此newArr修改空间中内容之后,arr也可以看到修改的结果
- int[] arr = {1,2,3,4,5,6};
- int[] newArr = arr;
- newArr[0] = 10;
- System.out.println("newArr: " + Arrays.toString(arr));
- // 使用Arrays中copyOf方法完成数组的拷贝:
- // copyOf方法在进行数组拷贝时,创建了一个新的数组
- // arr和newArr引用的不是同一个数组
- arr[0] = 1;
- newArr = Arrays.copyOf(arr, arr.length);
- System.out.println("newArr: " + Arrays.toString(newArr));
- // 因为arr修改其引用数组中内容时,对newArr没有任何影响
- arr[0] = 10;
- System.out.println("arr: " + Arrays.toString(arr));
- System.out.println("newArr: " + Arrays.toString(newArr));
- // 拷贝某个范围.
- int[] newArr2 = Arrays.copyOfRange(arr, 2, 4);
- System.out.println("newArr2: " + Arrays.toString(newArr2));
- }
、
- public static int[] copyOf(int[] arr) {
- int[] ret = new int[arr.length];
- for (int i = 0; i < arr.length; i++) {
- ret[i] = arr[i];
- }
- return ret;
- }
- public static void main(String[] args) {
- int[] arr = {1,2,3,4,5,6};
- System.out.println(avg(arr));
- }
- public static double avg(int[] arr) {
- int sum = 0;
- for (int x : arr) {
- sum += x;
- }
- return (double)sum / (double)arr.length;
- }
- // 执行结果
- 3.5
- public static void main(String[] args) {
- int[] arr = {1,2,3,10,5,6};
- System.out.println(find(arr, 10));
- }
- public static int find(int[] arr, int data) {
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] == data) {
- return i;
- }
- }
- return -1; // 表示没有找到
- }
-
- // 执行结果
- 3
- public static void main(String[] args) {
- int[] arr = {1,2,3,4,5,6};
- System.out.println(binarySearch(arr, 6));
- }
- public static int binarySearch(int[] arr, int toFind) {
- int left = 0;
- int right = arr.length - 1;
- while (left <= right) {
- int mid = (left + right) / 2;
- if (toFind < arr[mid]) {
- // 去左侧区间找
- right = mid - 1;
- } else if (toFind > arr[mid]) {
- // 去右侧区间找
- left = mid + 1;
- } else {
- // 相等, 说明找到了
- return mid;
- }
- }
- // 循环结束, 说明没找到
- return -1;
- }
- // 执行结果
- 5
- public static void main(String[] args) {
- int[] arr = {9, 5, 2, 7};
- bubbleSort(arr);
- System.out.println(Arrays.toString(arr));
- }
- public static void bubbleSort(int[] arr) {
- for (int i = 0; i < arr.length; i++) {
- for (int j = 1; j < arr.length-i; j++) {
- if (arr[j-1] > arr[j]) {
- int tmp = arr[j - 1];
- arr[j - 1] = arr[j];
- arr[j] = tmp;
- }
- }
- } // end for
- } // end bubbleSort
-
-
- // 执行结果
- [2, 5, 7, 9]
- public static void main(String[] args) {
- int[] arr = {9, 5, 2, 7};
- Arrays.sort(arr);
- System.out.println(Arrays.toString(arr));
- }
- public static void main(String[] args) {
- int[] arr = {1, 2, 3, 4};
- reverse(arr);
- System.out.println(Arrays.toString(arr));
- }
- public static void reverse(int[] arr) {
- int left = 0;
- int right = arr.length - 1;
- while (left < right) {
- int tmp = arr[left];
- arr[left] = arr[right];
- arr[right] = tmp;
- left++;
- right--;
- }
- }
数据类型[][] 数组名称 = new 数据类型 [行数][列数] { 初始化数据 };
- int[][] arr = {
- {1, 2, 3, 4},
- {5, 6, 7, 8},
- {9, 10, 11, 12}
- };
- for (int row = 0; row < arr.length; row++) {
- for (int col = 0; col < arr[row].length; col++) {
- System.out.printf("%d\t", arr[row][col]);
- }
- System.out.println("");
- }
-
- // 执行结果
- 1 2 3 4
- 5 6 7 8
- 9 10 11 12