二维数组和算法
1、⼆维数组
1. 定义: int[][] scores = new int[][]{{80,90,100},{99,88,77},{66,100,88}};
2. 简写:int[][] scores = {{80,90,100},{99,88,77},{66,100,88}};
3. 定义⼆: int[][] scores = new int[3][3];
4. 例⼦:
import java.util.Scanner; public class Demo01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[][] name = new String[3][3]; //赋值 for (int i = 0; i < name.length; i++) {//i代表小组 for (int j = 0; j < name[i].length; j++) { System.out.print("第" + (i + 1) + "组第" + (j + 1) + "个成员姓名为:"); name[i][j] = scanner.next(); } } for (int i = 0; i < name.length; i++) {//i代表小组 for (int j = 0; j < name[i].length; j++) { System.out.println("第" + (i + 1) + "组第" + (j + 1) + "个成员姓名为:" + name[i][j]); } } } }
2、 顺序查询
1. 需求:有⼀个数组,找⼀个数在不在这个数组中,如果在打印它的下标,如果不在,打印不存在。
2.
import java.util.Scanner; public class Demo02 { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6}; Scanner sc = new Scanner(System.in); System.out.print("请输入要查询的数:"); int valuce = sc.nextInt(); int index = -1; for (int i = 0; i < arr.length; i++) { if (valuce == arr[i]) { index = i; } } if (index == -1) { System.out.println("不存在"); } else { System.out.println("存在,下标为:" + index); } } }
3. 冒泡排序
1. ⽐较经典的排序算法,⽐较两个相邻的元素,将较⼤的元素换到右边(后边,下边)。
2.
//冒泡排序 public class Demo03 { public static void main(String[] args) { int[] arr = {99, 85, 79, 99, 69, 87}; for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j < arr.length; j++) { if (arr[i] > arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
4. ⼆分法
1. 前提是有序序列
2.
public class Demo04 { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int value = 6; int index = -1; int right = arr.length - 1; int left = 0; int mid = (right + left) / 2; while (left <= right) { if (value == arr[mid]) { index = mid; break; } else if (value > arr[mid]) { left = mid + 1; } else if (value < arr[mid]) { right = mid - 1; } mid = (right + left) / 2; } if (index == -1) { System.out.println("不存在"); } else { System.out.println("存在,下标为" + index); } } }
5. arrays类
1.
2.
import java.util.Arrays; public class Demo05 { public static void main(String[] args) { int[] arr1={10,20,30}; int[] arr2={10,20,30}; int[] arr3={10,40,30}; System.out.println(Arrays.equals(arr1,arr2));//equals(数组1,数组2)比较两个数组是否相等 System.out.println(Arrays.equals(arr1,arr3)); System.out.println(Arrays.toString(arr1));//toString(数组)将一个数组转换为一个字符串 int[] arr4=new int[3]; Arrays.fill(arr4,100);//fill(数组,值)把数组中所有的元素都赋值为一个统一的值 System.out.println(Arrays.toString(arr4)); int[] arr5={10,20,30,40}; int[] arr6=Arrays.copyOf(arr5,2);//copyOf(arr,length)把数组arr复制成一个长度为length的新数组 System.out.println(Arrays.toString(arr6)); Arrays.sort(arr5); System.out.println(Arrays.toString(arr5)); System.out.println(Arrays.binarySearch(arr5,50));//binarySearch(arr,val)程序元素val的下标需要数组为有序的 } }
3. 练习
import java.util.Scanner; public class Demo06 { public static void main(String[] args) { int[][] scores = new int[3][5]; int[] sum = new int[3]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < scores.length; i++) { System.out.println("***********第" + (i + 1) + "个班***********"); for (int j = 0; j < scores[i].length; j++) { System.out.print("请输入第" + (j + 1) + "个学生的成绩:"); scores[i][j] = scanner.nextInt(); sum[i] += scores[i][j]; } } System.out.println("***********成绩统计***********"); for (int i = 0; i < sum.length; i++) { System.out.println((i + 1) + "班总成绩:" + sum[i]); } } }