深圳幻海软件技术有限公司 欢迎您!

java二维数组

2023-03-30

目录1、二维数组2、二维数组的定义3、练习:1. 定义一个字符串的二维数组,三行两列,数组每一个元素值循环输入6个人的姓名,再使用循环输出这6个人的姓名。 2.将一个整形二维数组赋值为如下形式:3.在如下整形数组中//1392//2763//5411//6882//求两条对角线上

目录

1、二维数组

2、二维数组的定义

3、练习:

1. 定义一个字符串的二维数组,三行两列,数组每一个元素值循环输入6个人的姓名,再使用循环输出这6个人的姓名。

 2.将一个整形二维数组赋值为如下形式:

3.在如下整形数组中 //1 3 9 2 // 2 7 6 3 // 5 4 1 1 // 6 8 8 2 // 求两条对角线上的和

4.//6 判断二维数组是否为对称数组 // {1,2,3,4}, // {2,1,4,3}, // {3,4,1,2}, // {4,3,2,1}

5.用二维数组模拟出扫雷界面

6.打印8*8的杨辉三角

7.随机生成一个二维数组,判断其中是否有鞍点

8.随机生成一个二维数组,必须要有鞍点

9.用数组做一个简易的商品管理系统


1、二维数组

a、了解二维数组的使用场景
        游戏中的地图
        数学计算( 矩阵 )
b、掌握二维数组的定义
c、掌握二维数组的遍历
d、常见的算法
        杨辉三角
        鞍点

2、二维数组的定义

int [ ] [ ] arr = new int [5][6];
第一个中括号表示的是行
第二个中括号表示的是列
数组的默认值: 0

静态定义二维数组  

int [ ][ ] arr = new int [ ] [ ] {
                {1},
                {1,2},
                {1,2,3},
                {1,2,3,4},
                {1,2,3,4,5},
};
int [ ] [ ] arr  =  new  int [ 5 ] [ ];
System.out.println(arr.length);

 

3、练习:

1. 定义一个字符串的二维数组,三行两列,数组每一个元素值循环输入6个人的姓名,再使用循环输出这6个人的姓名。

        格式要求:

                         XXX XXX

                         XXX XXX

                         XXX XXX

  1. import java.util.Scanner;
  2. public class test02 {
  3. public static void main(String[] args) {
  4. String array[][] = new String[3][2];
  5. int count = 1;
  6. for (int i = 0; i < array.length; i++) {
  7. for (int j = 0; j < array[i].length; j++) {
  8. System.out.print("请输入第" + count + "位同学的姓名:");
  9. Scanner sc = new Scanner(System.in);
  10. String name = sc.next();
  11. array[i][j] = name;
  12. count++;
  13. }
  14. }
  15. for (int i = 0; i < array.length; i++) {
  16. for (int j = 0; j < array[i].length; j++) {
  17. System.out.print(array[i][j] + "\t");
  18. }
  19. System.out.println();
  20. }
  21. }
  22. }

 2.将一个整形二维数组赋值为如下形式:

                                                                1  2  3     4

                                                                2  4  6     8

                                                                3  6  9    12

                                                                4  8  12  16

  1. public class test03 {
  2. public static void main(String[] args) {
  3. //将一个整形二维数组赋值为如下形式:
  4. // 1 2 3 4
  5. //2 4 6 8
  6. //3 6 9 12
  7. //4 8 12 16
  8. int array[][] = new int[4][4];
  9. for (int i = 0; i < array.length; i++) {
  10. int num = 0;
  11. for (int j = 0; j < array[i].length; j++) {
  12. array[i][j] = (i + 1) * (j + 1);
  13. }
  14. }
  15. for (int i = 0; i < array.length; i++) {
  16. for (int j = 0; j < array[i].length; j++) {
  17. System.out.print(array[i][j] + "\t");
  18. }
  19. System.out.println();
  20. }
  21. }
  22. }

3.在如下整形数组中 //1 3 9 2 // 2 7 6 3 // 5 4 1 1 // 6 8 8 2 // 求两条对角线上的和

  1. public class test04 {
  2. public static void main(String[] args) {
  3. //在如下整形数组中
  4. // 1 3 9 2
  5. // 2 7 6 3
  6. // 5 4 1 1
  7. // 6 8 8 2
  8. // 求两条对角线上的和
  9. int array[][] = new int[][]{
  10. {1, 3, 9, 2},
  11. {2, 7, 6, 3},
  12. {5, 4, 1, 1},
  13. {6, 8, 8, 2}
  14. };
  15. int sum1 = 0;
  16. int sum2 = 0;
  17. for (int i = 0; i < array.length; i++) {
  18. sum1 = sum1 + array[i][i];
  19. sum2 = sum2 + array[array.length - i - 1][i];
  20. }
  21. System.out.println(sum1);
  22. System.out.println(sum2);
  23. System.out.println("两条对角线上的和位:" + (sum1 + sum2));
  24. }
  25. }

4.//6 判断二维数组是否为对称数组 // {1,2,3,4}, // {2,1,4,3}, // {3,4,1,2}, // {4,3,2,1}

  1. public class test05 {
  2. public static void main(String[] args) {
  3. //6 判断二维数组是否为对称数组
  4. // {1,2,3,4},
  5. // {2,1,4,3},
  6. // {3,4,1,2},
  7. // {4,3,2,1}
  8. int[][] array = new int[][]{
  9. {1, 2, 3, 4},
  10. {2, 1, 4, 3},
  11. {3, 4, 1, 2},
  12. {4, 3, 2, 1}
  13. };
  14. boolean flag = true;
  15. for (int i = 0; i < array.length; i++) {
  16. for (int j = 0; j < array[i].length; j++) {
  17. if (array[i][j] != array[j][i]) {
  18. flag = false;
  19. break;
  20. }
  21. }
  22. }
  23. if (flag == true) {
  24. System.out.println("该数组为对称数组");
  25. } else {
  26. System.out.println("该数组为非对称数组");
  27. }
  28. }
  29. }

5.用二维数组模拟出扫雷界面

        考虑 :设定总共多少个雷,以及每个雷的坐标位置由随机数决定

  1. import java.util.Random;
  2. import java.util.Scanner;
  3. public class test06 {
  4. public static void main(String[] args) {
  5. //用二维数组模拟出扫雷界面
  6. // 如5*5的界面
  7. // * _ _ _ *
  8. // _ _ * _ _
  9. // _ _ _ _ _
  10. // _ * _ _ _
  11. // _ _ _ * _
  12. // 考虑 :设定总共多少个雷,以及每个雷的坐标位置由随机数决定
  13. String array[][] = new String[5][5];
  14. Random random = new Random();
  15. for (int i = 0; i < array.length; i++) {
  16. for (int j = 0; j < array[i].length; j++) {
  17. int num = random.nextInt(5);
  18. if (num == 0) {
  19. array[i][j] = "*";
  20. } else {
  21. array[i][j] = "_";
  22. }
  23. }
  24. }
  25. for (int i = 0; i < array.length; i++) {
  26. for (int j = 0; j < array[i].length; j++) {
  27. System.out.print(array[i][j] + "\t");
  28. }
  29. System.out.println();
  30. }
  31. }
  32. }

6.打印8*8的杨辉三角

  1. public class yangHuiTriangle {
  2. public static void main(String[] args) {
  3. yanghui();
  4. }
  5. public static void yanghui() {
  6. //1 定义一个二维数组8*8 arr[8][8]
  7. int arr[][] = new int[8][8];
  8. //2 arr[0..7][0] == 1 arr[i][i] = 1;
  9. for (int i = 0; i < arr.length; i++) {
  10. arr[i][0] = arr[i][i] = 1;
  11. }
  12. //3 从第三行开始,第二列开始 arr[2][1] = arr[2-1][1] + arr[2-1][1-1];
  13. for (int i = 2; i < arr.length; i++) {
  14. for (int j = 1; j < i; j++) {
  15. arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
  16. }
  17. }
  18. //4 打印
  19. for (int i = 0; i < arr.length; i++) {
  20. for (int j = 0; j < i + 1; j++) {
  21. System.out.print(arr[i][j] + "\t");
  22. }
  23. System.out.println();
  24. }
  25. }
  26. }

7.随机生成一个二维数组,判断其中是否有鞍点

  1. import java.util.Random;
  2. public class saddlePoint01 {
  3. //定义一个数组
  4. static int arr[][] = new int[5][5];
  5. public static void main(String[] args) {
  6. saddle();
  7. }
  8. //生成一个随机数组
  9. public static void getArr() {
  10. Random random = new Random();
  11. for (int i = 0; i < arr.length; i++) {
  12. for (int j = 0; j < arr[i].length; j++) {
  13. int a = random.nextInt(100);
  14. arr[i][j] = a;
  15. }
  16. }
  17. for (int i = 0; i < arr.length; i++) {
  18. for (int j = 0; j < arr[i].length; j++) {
  19. System.out.print(arr[i][j] + "\t");
  20. }
  21. System.out.println();
  22. }
  23. }
  24. //判断鞍点存不存再
  25. public static void saddle() {
  26. getArr();
  27. // 1、找第一行的最大值arr[0][2] 0, 2
  28. boolean flag = false;
  29. for (int i = 0; i < arr.length; i++) {
  30. int rowindex = 0;
  31. int max = arr[i][0];
  32. for (int j = 1; j < arr[i].length; j++) {
  33. if (max < arr[i][j]) {
  34. max = arr[i][j];
  35. rowindex = j;
  36. }
  37. }
  38. //2.看这个数在这一列中是不是最小的(难点)
  39. int colindex = 0;
  40. int num = arr[i][rowindex];
  41. for (int j = 0; j < arr.length; j++) {
  42. if (num > arr[j][rowindex]) {
  43. num = arr[j][rowindex];
  44. }
  45. }
  46. if (num == arr[i][rowindex]) {
  47. System.out.println("鞍点是:" + num);
  48. flag = true;
  49. }
  50. }
  51. if (!flag) {
  52. System.out.println("该数列没有鞍点");
  53. }
  54. }
  55. }

8.随机生成一个二维数组,必须要有鞍点

  1. import java.util.Random;
  2. public class saddlePoint02 {
  3. //定义一个数组
  4. static int arr[][] = new int[5][5];
  5. static boolean flag = false;
  6. public static void main(String[] args) {
  7. //用一个循环 直到找到鞍点后 循环结束
  8. while (flag == false) {
  9. getArr();
  10. saddle();
  11. }
  12. }
  13. //生成一个随机数组
  14. public static void getArr() {
  15. Random random = new Random();
  16. for (int i = 0; i < arr.length; i++) {
  17. for (int j = 0; j < arr[i].length; j++) {
  18. int a = random.nextInt(100);
  19. arr[i][j] = a;
  20. }
  21. }
  22. }
  23. //判断鞍点是否存在
  24. public static void saddle() {
  25. // 1、找第一行的最大值arr[0][2] 0, 2
  26. for (int i = 0; i < arr.length; i++) {
  27. int rowindex = 0;
  28. int max = arr[i][0];
  29. for (int j = 1; j < arr[i].length; j++) {
  30. if (max < arr[i][j]) {
  31. max = arr[i][j];
  32. rowindex = j;
  33. }
  34. }
  35. //2.看这个数在这一列中是不是最小的(难点)
  36. int colindex = 0;
  37. int num = arr[i][rowindex];
  38. for (int j = 0; j < arr.length; j++) {
  39. if (num > arr[j][rowindex]) {
  40. num = arr[j][rowindex];
  41. }
  42. }
  43. if (num == arr[i][rowindex]) {
  44. System.out.println("鞍点是:" + num);
  45. flag = true;
  46. }
  47. }
  48. if (!flag) {
  49. System.out.println("该数列没有鞍点");
  50. }
  51. }
  52. }

9.用数组做一个简易的商品管理系统

要求一些基本功能:

  • 显示商品
  • 增加商品
  • 删除商品
  • 查询商品
  • 插入商品
  • 对商品按价格进行排序
  • 退出
  1. import java.util.Scanner;
  2. public class CommodityManagementSystem {
  3. //定义数组长度
  4. static int len = 100;
  5. //定义商品编号
  6. static int[] nos = new int[len];
  7. //定义商品名称
  8. static String[] names = new String[len];
  9. //定义商品价格
  10. static double[] prices = new double[len];
  11. //定义数组当前长度
  12. static int index;
  13. public static void main(String[] args) {
  14. init();
  15. showMenu();
  16. }
  17. //初始化数组
  18. public static void init() {
  19. //定义一组虚拟商品进行测试
  20. nos[0] = 1;
  21. names[0] = "苹果";
  22. prices[0] = 11.5;
  23. nos[1] = 2;
  24. names[1] = "香蕉";
  25. prices[1] = 1.5;
  26. nos[2] = 3;
  27. names[2] = "白菜";
  28. prices[2] = 3.8;
  29. nos[3] = 4;
  30. names[3] = "西瓜";
  31. prices[3] = 20;
  32. nos[4] = 5;
  33. names[4] = "怡宝";
  34. prices[4] = 2;
  35. index = 5;
  36. }
  37. //主菜单
  38. public static void showMenu() {
  39. Scanner sc = new Scanner(System.in);
  40. System.out.println("******主菜单*********");
  41. String menu[] = new String[]{
  42. "1.显示商品",
  43. "2.增加商品",
  44. "3.删除商品",
  45. "4.查询商品",
  46. "5.插入商品",
  47. "6.对商品按价格进行排序",
  48. "7.退出"
  49. };
  50. for (String s : menu) {
  51. System.out.println(s);
  52. }
  53. while (true) {
  54. System.out.print("请选择你要进行的操作:");
  55. int num = sc.nextInt();
  56. switch (num) {
  57. case 1:
  58. showProduct();
  59. break;
  60. case 2:
  61. addProduct();
  62. break;
  63. case 3:
  64. delProduct();
  65. break;
  66. case 4:
  67. selectProduct();
  68. break;
  69. case 5:
  70. insetProduct();
  71. break;
  72. case 6:
  73. sortProduct();
  74. break;
  75. case 7:
  76. return;
  77. default:
  78. System.out.println("输入错误!");
  79. break;
  80. }
  81. }
  82. }
  83. //显示商品
  84. public static void showProduct() {
  85. System.out.println("编号\t名称\t价格\t");
  86. for (int i = 0; i < index; i++) {
  87. System.out.println(nos[i] + "\t\t" + names[i] + "\t" + prices[i]);
  88. }
  89. }
  90. //增加商品
  91. public static void addProduct() {
  92. Scanner sc = new Scanner(System.in);
  93. System.out.print("请输入编号:");
  94. int no = sc.nextInt();
  95. System.out.print("请输入名称:");
  96. String name = sc.next();
  97. System.out.print("请输入价格:");
  98. double price = sc.nextDouble();
  99. nos[index] = no;
  100. names[index] = name;
  101. prices[index] = price;
  102. index++;
  103. }
  104. //删除商品
  105. public static void delProduct() {
  106. Scanner sc = new Scanner(System.in);
  107. System.out.print("请选择你要删除的商品编号:");
  108. int no = sc.nextInt();
  109. int pos = -1;
  110. for (int i = 0; i < index; i++) {
  111. if (i == no - 1) {
  112. pos = i;
  113. break;
  114. }
  115. }
  116. if (pos == -1) {
  117. System.out.println("你要删除的商品不存在!");
  118. } else {
  119. for (int i = pos; i <= index; i++) {
  120. names[i] = names[i + 1];
  121. prices[i] = prices[i + 1];
  122. }
  123. index--;
  124. }
  125. }
  126. //查找商品
  127. public static void selectProduct() {
  128. Scanner sc = new Scanner(System.in);
  129. System.out.print("请输入你要查的商品名称:");
  130. String name = sc.next();
  131. int pos = -1;
  132. for (int i = 0; i < index; i++) {
  133. if (names[i].equals(name)) {
  134. System.out.println("正在查找中……");
  135. pos = i;
  136. break;
  137. }
  138. }
  139. if (pos == -1) {
  140. System.out.println("很遗憾,未找到你需要的商品!");
  141. } else {
  142. System.out.println("*********查询到的商品**********");
  143. System.out.println("编号\t名称\t价格\t");
  144. System.out.println(nos[pos] + "\t\t" + names[pos] + "\t" + prices[pos]);
  145. }
  146. }
  147. //插入商品
  148. public static void insetProduct() {
  149. Scanner sc = new Scanner(System.in);
  150. System.out.print("请选择你要插入的商品编号:");
  151. int no = sc.nextInt();
  152. int pos = -1;
  153. for (int i = 0; i < index; i++) {
  154. if (i == no - 1) {
  155. pos = i;
  156. break;
  157. }
  158. }
  159. if (pos == -1) {
  160. System.out.println("你要插入的位置不存在!");
  161. } else {
  162. for (int i = index; i > pos; i--) {
  163. names[i] = names[i - 1];
  164. prices[i] = prices[i - 1];
  165. }
  166. System.out.print("你要插入的商品名称:");
  167. String name = sc.next();
  168. System.out.print("你要插入的商品价格为:");
  169. double price = sc.nextDouble();
  170. names[pos] = name;
  171. prices[pos] = price;
  172. index++;
  173. nos[index - 1] = index;
  174. }
  175. }
  176. //对商品进行排序
  177. public static void sortProduct() {
  178. double pritemp;
  179. String nametemp;
  180. for (int i = 0; i < index - 1; i++) {
  181. boolean flag = false;
  182. for (int j = 0; j < index - i - 1; j++) {
  183. if (prices[j] > prices[j + 1]) {
  184. pritemp = prices[j];
  185. nametemp = names[j];
  186. prices[j] = prices[j + 1];
  187. names[j] = names[j + 1];
  188. prices[j + 1] = pritemp;
  189. names[j + 1] = nametemp;
  190. flag = true;
  191. }
  192. }
  193. if (!flag) {
  194. break;
  195. }
  196. }
  197. }
  198. }

 

文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树首页概览42515 人正在系统学习中