🎄数组练习
🎅二分查找
public class 二分查找 { public static int binarySearch(int []arr, int target) { int left = 0; int right = arr.length-1; while (left <= right) { int mid=(left+right)/2; if (arr[mid]<target){ left = mid+1; } else if (arr[mid]>target){ right = mid-1; } else return mid+1; } System.out.println("没有这个数"); return -1; } public static void main(String[] args) { int target = 8; int arr[] = {1,2,3,4,5,6,7,8}; System.out.println("要查找的数是第"+binarySearch(arr,target)+"个"); } }
🎅冒泡排序
import java.util.Arrays; public class 冒泡排序 { public static int[] bubble(int arr[]) { for (int i = 0; i < arr.length-1; i++){ for (int j = 0; j < arr.length-1-i; j++){ if (arr[j]>arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } return arr; } public static void main(String[] args) { int arr [] = {1,1,4,7,2,4,6,9,2,6,5}; System.out.println(Arrays.toString(bubble(arr))); } }
🎅判断数组是否有序
public class 判断数组是否有序 { public static boolean judgment(int arr[]) { for (int i = 0; (i+1)<arr.length; i++){ if (arr[i]>arr[i+1]) return false; } return true; } public static void main(String[] args) { int arr1[] = {1,2,3,4}; int arr2[] = {2,1,4,3}; if (judgment(arr1)) System.out.println("arr1"+"有序"); else System.out.println("arr1"+"无序"); if (judgment(arr2)) System.out.println("arr2"+"有序"); else System.out.println("arr2"+"无序"); } }
🎅只出现一次的元素
public class 只出现一次的数字 { public static int Only(int arr[]) { int tmp = 0; for (int i = 0; i < arr.length; i++) { /* 涉及到异或运算,对于这道题,你应该了解异或有什么样性质 1. 任何数与0异或,都为原数 2. 与本身自己异或,则为0 3. 交换律和结合律(解决这道题的关键),你把整个循环的计算写成数学运算过程,你会发现要利用交换律和结合律,可以实现这道题的要求 */ tmp ^= arr[i]; } return tmp; } public static void main(String[] args) { int arr[] = {2,0,0,1,1,2,6}; System.out.println(Only(arr)); } }
🎅改变原有数组的值
import java.util.*; public class 改变原有数组元素的值 { public static int[] transform(int[]array2) { for (int i = 0; i < array2.length; i++) { array2[i] *=2; } return array2; } public static void main(String[] args) { int [] array1 = {1,2,3,}; System.out.println(Arrays.toString(array1)); System.out.println("×2改变之后数组="+ Arrays.toString(transform(array1))); } }
🎅数组所以元素之和
public class 数组所有元素之和 { public static float sum(int[]array) { int sum = 0; for (int i = 0; i < array.length; i++) sum += array[i]; return sum; } public static void main(String[] args) { int [] array = {1,2,3,}; for (int i = 0; i < array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("所以元素之和="+sum(array)); } }
🎅 数组拷贝
import java.util.Arrays; public class 数组的拷贝 { public static int[] copyOf(int []arr1) { int []arr2; arr2 = new int[arr1.length]; for (int i = 0; i < arr1.length; i++) { arr2[i] = arr1[i]; } return arr2; } public static void main(String[] args) { int []arr1={1,2,3,4,5}; System.out.println("arr1="+Arrays.toString(arr1)); System.out.println("arr2="+Arrays.toString(copyOf(arr1))); } }
🎅数组转字符串
public class 数组转字符串 { public static void printString(int[]arr) { String a ="["; for (int i = 0; i < arr.length; i++){ a+=arr[i]; if(i != arr.length -1) a += ","; } a+="]"; System.out.println(a); } public static void main(String[] args) { int arr[] = {1,2,3,4,5}; printString(arr); } }
🎅求数组元素平均值
public class 求数组的平均值 { public static float avg(int[]array) { float avg; int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } avg = sum / array.length; return avg; } public static void main(String[] args) { int [] array = {1,2,3,}; for (int i = 0; i < array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("平均数="+avg(array)); } }
🎄二维数组
二维数组本质上也就是一维数组, 只不过每个元素又是一个一维数组.
基本语法
数据类型[][] 数组名称 = new 数据类型 [行数][列数] { 初始化数据 };
代码示例
二维数组的用法和一维数组并没有明显差别, 因此我们不再赘述.
不过要注意一个点,C里每行数组会跟列对齐,会自动补0,java里不会自动补,如下代码
import java.util.Arrays; public class test { public static void main(String[] args) { int[][] arr; arr = new int[][]{ {1, 2, 3}, {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 // 5 6 7 8 // 9 10 11 12 } }