Java基础入门day14
day14
内容回顾
冒泡排序
public class BubbleSort{ public static void main(String[] args){ int nums[] = {12, 33, 111, 35, 1}; for(int i = 0; i < nums.length -1; i++){ for(int j = 0; j < nums.length - 1 - i; j++){ int te; if(nums[j] > nums[j + 1]){ te = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = te; } } } System.out.println("排序后:"); for(int i = 0; i < nums.length; i++){ System.out.print(nums[i] + "\t"); } System.out.println(); } }
java api中的排序
package com.saas; import java.util.Arrays; public class ArraySort { public static void main(String[] args) { int[] nums = {111, 33, 12, 4, 88, 34}; for (int i = 0; i < nums.length; i++) { int num = nums[i]; System.out.print(num + "\t"); } Arrays.sort(nums); System.out.println("\n=============排序后正序输出:"); for (int i = 0; i < nums.length; i++) { int num = nums[i]; System.out.print(num + "\t"); } System.out.println("\n=============排序后逆序输出:"); for(int i = nums.length - 1; i >= 0; i--){ int num = nums[i]; System.out.print(num + "\t"); } } }我们也可以自定义方法,通过调用自己写好的方法完成排序
package com.saas; public class ArraySort02 { public static void main(String[] args) { int[] nums = {111, 33, 12, 4, 88, 34, 11, 54, 999}; nums = bubbleSort(nums); for (int i = 0; i < nums.length; i++) { int num = nums[i]; System.out.print(num + "\t"); } System.out.println(); } public static int[] bubbleSort(int[] nums){ for(int j = 0; j < nums.length - 1; j++){ for(int i = 0; i < nums.length - 1 - j; i++){ int temp; if(nums[i] > nums[i + 1]){ temp = nums[i]; nums[i] = nums[i + 1]; nums[i + 1] = temp; } } } return nums; } }
二维数组
概念:一维数组中的一维数组,即数组的元素还是数组
姓名 年龄 sex 张三 20 m 李四 18 f 王五 22 m 跟Excel类似,当我们需要查找Excel中的数据单元格时,需要两个下标,行和列,二维数组相当于一个多行多列的表格
通过行号和列号确定某一个元素的数据
声明与赋值:
package com.saas; public class MultiArray01 { public static void main(String[] args) { int[][] nums = new int[3][5]; for (int i = 0; i < nums.length; i++) { int[] num = nums[i]; for (int j = 0; j < num.length; j++) { int i1 = num[j]; System.out.print(i1 + "\t"); } System.out.println(); } System.out.println("部分赋值后:"); nums[0][0] = 10; nums[0][2] = 78; nums[2][4] = 43; for (int i = 0; i < nums.length; i++) { int[] num = nums[i]; for (int j = 0; j < num.length; j++) { int i1 = num[j]; System.out.print(i1 + "\t"); } System.out.println(); } } }
二维数组案例
俄罗斯方块游戏
package com.saas; public class RussianShape01 { public static void main(String[] args) { int[][] shapes = getRandomShape(); printShape(shapes); // for (int i = 0; i < shapes.length; i++) { // int[] shape = shapes[i]; // for (int j = 0; j < shape.length; j++) { // int i1 = shape[j]; // System.out.print(i1 + "\t"); // } // System.out.println(); // } } public static void printShape(int[][] shapes){ for (int i = 0; i < shapes.length; i++) { for (int j = 0; j < shapes.length; j++) { boolean flag = false; for (int k = 0; k < shapes.length; k++) { if(shapes[k][0] == i && shapes[k][1] == j){ flag = true; break; } } if(flag){ System.out.print("■ "); }else { System.out.print("□ "); } } System.out.println(); } } public static int[][] getRandomShape(){ int shape = (int)(Math.random() * 7); int type = (int)(Math.random() * 4); switch (shape){ case 0: // 方块 System.out.println("方块"); return new int[][]{{0, 0}, {0, 1}, {1, 0}, {1, 1}}; case 1: // 长条 switch (type){ case 0: case 2: // 竖条 System.out.println("竖条"); return new int[][]{{0, 0}, {1, 0}, {2, 0}, {3, 0}}; default: // 横条 System.out.println("横条"); return new int[][]{{0, 0}, {0, 1}, {0, 2}, {0, 3}}; } default: switch (type){ case 0: System.out.println("T"); return new int[][]{{0, 0}, {0, 1}, {0, 2}, {1, 1}}; case 1: System.out.println("-|"); return new int[][]{{0, 1}, {1, 0}, {1, 1}, {2, 1}}; case 2: System.out.println("土"); return new int[][]{{0, 1}, {1, 0}, {1, 1}, {1, 2}}; default: System.out.println("|-"); return new int[][]{{0, 0}, {1, 0}, {2, 0}, {1, 1}}; } } } }