目录
1. 定义一个字符串的二维数组,三行两列,数组每一个元素值循环输入6个人的姓名,再使用循环输出这6个人的姓名。
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}
1、二维数组
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
import java.util.Scanner;
public class test02 {
public static void main(String[] args) {
String array[][] = new String[3][2];
int count = 1;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print("请输入第" + count + "位同学的姓名:");
Scanner sc = new Scanner(System.in);
String name = sc.next();
array[i][j] = name;
count++;
}
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
}
}
2.将一个整形二维数组赋值为如下形式:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
public class test03 {
public static void main(String[] args) {
//将一个整形二维数组赋值为如下形式:
// 1 2 3 4
// 2 4 6 8
// 3 6 9 12
// 4 8 12 16
int array[][] = new int[4][4];
for (int i = 0; i < array.length; i++) {
int num = 0;
for (int j = 0; j < array[i].length; j++) {
array[i][j] = (i + 1) * (j + 1);
}
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
}
}
3.在如下整形数组中 //1 3 9 2 // 2 7 6 3 // 5 4 1 1 // 6 8 8 2 // 求两条对角线上的和
public class test04 {
public static void main(String[] args) {
//在如下整形数组中
// 1 3 9 2
// 2 7 6 3
// 5 4 1 1
// 6 8 8 2
// 求两条对角线上的和
int array[][] = new int[][]{
{1, 3, 9, 2},
{2, 7, 6, 3},
{5, 4, 1, 1},
{6, 8, 8, 2}
};
int sum1 = 0;
int sum2 = 0;
for (int i = 0; i < array.length; i++) {
sum1 = sum1 + array[i][i];
sum2 = sum2 + array[array.length - i - 1][i];
}
System.out.println(sum1);
System.out.println(sum2);
System.out.println("两条对角线上的和位:" + (sum1 + sum2));
}
}
4.//6 判断二维数组是否为对称数组 // {1,2,3,4}, // {2,1,4,3}, // {3,4,1,2}, // {4,3,2,1}
public class test05 {
public static void main(String[] args) {
//6 判断二维数组是否为对称数组
// {1,2,3,4},
// {2,1,4,3},
// {3,4,1,2},
// {4,3,2,1}
int[][] array = new int[][]{
{1, 2, 3, 4},
{2, 1, 4, 3},
{3, 4, 1, 2},
{4, 3, 2, 1}
};
boolean flag = true;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (array[i][j] != array[j][i]) {
flag = false;
break;
}
}
}
if (flag == true) {
System.out.println("该数组为对称数组");
} else {
System.out.println("该数组为非对称数组");
}
}
}
5.用二维数组模拟出扫雷界面
考虑 :设定总共多少个雷,以及每个雷的坐标位置由随机数决定
import java.util.Random;
import java.util.Scanner;
public class test06 {
public static void main(String[] args) {
//用二维数组模拟出扫雷界面
// 如5*5的界面
// * _ _ _ *
// _ _ * _ _
// _ _ _ _ _
// _ * _ _ _
// _ _ _ * _
// 考虑 :设定总共多少个雷,以及每个雷的坐标位置由随机数决定
String array[][] = new String[5][5];
Random random = new Random();
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
int num = random.nextInt(5);
if (num == 0) {
array[i][j] = "*";
} else {
array[i][j] = "_";
}
}
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
}
}
6.打印8*8的杨辉三角
public class yangHuiTriangle {
public static void main(String[] args) {
yanghui();
}
public static void yanghui() {
//1 定义一个二维数组8*8 arr[8][8]
int arr[][] = new int[8][8];
//2 arr[0..7][0] == 1 arr[i][i] = 1;
for (int i = 0; i < arr.length; i++) {
arr[i][0] = arr[i][i] = 1;
}
//3 从第三行开始,第二列开始 arr[2][1] = arr[2-1][1] + arr[2-1][1-1];
for (int i = 2; i < arr.length; i++) {
for (int j = 1; j < i; j++) {
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
}
//4 打印
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
7.随机生成一个二维数组,判断其中是否有鞍点
import java.util.Random;
public class saddlePoint01 {
//定义一个数组
static int arr[][] = new int[5][5];
public static void main(String[] args) {
saddle();
}
//生成一个随机数组
public static void getArr() {
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
int a = random.nextInt(100);
arr[i][j] = a;
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
//判断鞍点存不存再
public static void saddle() {
getArr();
// 1、找第一行的最大值arr[0][2] 0, 2
boolean flag = false;
for (int i = 0; i < arr.length; i++) {
int rowindex = 0;
int max = arr[i][0];
for (int j = 1; j < arr[i].length; j++) {
if (max < arr[i][j]) {
max = arr[i][j];
rowindex = j;
}
}
//2.看这个数在这一列中是不是最小的(难点)
int colindex = 0;
int num = arr[i][rowindex];
for (int j = 0; j < arr.length; j++) {
if (num > arr[j][rowindex]) {
num = arr[j][rowindex];
}
}
if (num == arr[i][rowindex]) {
System.out.println("鞍点是:" + num);
flag = true;
}
}
if (!flag) {
System.out.println("该数列没有鞍点");
}
}
}
8.随机生成一个二维数组,必须要有鞍点
import java.util.Random;
public class saddlePoint02 {
//定义一个数组
static int arr[][] = new int[5][5];
static boolean flag = false;
public static void main(String[] args) {
//用一个循环 直到找到鞍点后 循环结束
while (flag == false) {
getArr();
saddle();
}
}
//生成一个随机数组
public static void getArr() {
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
int a = random.nextInt(100);
arr[i][j] = a;
}
}
}
//判断鞍点是否存在
public static void saddle() {
// 1、找第一行的最大值arr[0][2] 0, 2
for (int i = 0; i < arr.length; i++) {
int rowindex = 0;
int max = arr[i][0];
for (int j = 1; j < arr[i].length; j++) {
if (max < arr[i][j]) {
max = arr[i][j];
rowindex = j;
}
}
//2.看这个数在这一列中是不是最小的(难点)
int colindex = 0;
int num = arr[i][rowindex];
for (int j = 0; j < arr.length; j++) {
if (num > arr[j][rowindex]) {
num = arr[j][rowindex];
}
}
if (num == arr[i][rowindex]) {
System.out.println("鞍点是:" + num);
flag = true;
}
}
if (!flag) {
System.out.println("该数列没有鞍点");
}
}
}
9.用数组做一个简易的商品管理系统
要求一些基本功能:
- 显示商品
- 增加商品
- 删除商品
- 查询商品
- 插入商品
- 对商品按价格进行排序
- 退出
import java.util.Scanner;
public class CommodityManagementSystem {
//定义数组长度
static int len = 100;
//定义商品编号
static int[] nos = new int[len];
//定义商品名称
static String[] names = new String[len];
//定义商品价格
static double[] prices = new double[len];
//定义数组当前长度
static int index;
public static void main(String[] args) {
init();
showMenu();
}
//初始化数组
public static void init() {
//定义一组虚拟商品进行测试
nos[0] = 1;
names[0] = "苹果";
prices[0] = 11.5;
nos[1] = 2;
names[1] = "香蕉";
prices[1] = 1.5;
nos[2] = 3;
names[2] = "白菜";
prices[2] = 3.8;
nos[3] = 4;
names[3] = "西瓜";
prices[3] = 20;
nos[4] = 5;
names[4] = "怡宝";
prices[4] = 2;
index = 5;
}
//主菜单
public static void showMenu() {
Scanner sc = new Scanner(System.in);
System.out.println("******主菜单*********");
String menu[] = new String[]{
"1.显示商品",
"2.增加商品",
"3.删除商品",
"4.查询商品",
"5.插入商品",
"6.对商品按价格进行排序",
"7.退出"
};
for (String s : menu) {
System.out.println(s);
}
while (true) {
System.out.print("请选择你要进行的操作:");
int num = sc.nextInt();
switch (num) {
case 1:
showProduct();
break;
case 2:
addProduct();
break;
case 3:
delProduct();
break;
case 4:
selectProduct();
break;
case 5:
insetProduct();
break;
case 6:
sortProduct();
break;
case 7:
return;
default:
System.out.println("输入错误!");
break;
}
}
}
//显示商品
public static void showProduct() {
System.out.println("编号\t名称\t价格\t");
for (int i = 0; i < index; i++) {
System.out.println(nos[i] + "\t\t" + names[i] + "\t" + prices[i]);
}
}
//增加商品
public static void addProduct() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入编号:");
int no = sc.nextInt();
System.out.print("请输入名称:");
String name = sc.next();
System.out.print("请输入价格:");
double price = sc.nextDouble();
nos[index] = no;
names[index] = name;
prices[index] = price;
index++;
}
//删除商品
public static void delProduct() {
Scanner sc = new Scanner(System.in);
System.out.print("请选择你要删除的商品编号:");
int no = sc.nextInt();
int pos = -1;
for (int i = 0; i < index; i++) {
if (i == no - 1) {
pos = i;
break;
}
}
if (pos == -1) {
System.out.println("你要删除的商品不存在!");
} else {
for (int i = pos; i <= index; i++) {
names[i] = names[i + 1];
prices[i] = prices[i + 1];
}
index--;
}
}
//查找商品
public static void selectProduct() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入你要查的商品名称:");
String name = sc.next();
int pos = -1;
for (int i = 0; i < index; i++) {
if (names[i].equals(name)) {
System.out.println("正在查找中……");
pos = i;
break;
}
}
if (pos == -1) {
System.out.println("很遗憾,未找到你需要的商品!");
} else {
System.out.println("*********查询到的商品**********");
System.out.println("编号\t名称\t价格\t");
System.out.println(nos[pos] + "\t\t" + names[pos] + "\t" + prices[pos]);
}
}
//插入商品
public static void insetProduct() {
Scanner sc = new Scanner(System.in);
System.out.print("请选择你要插入的商品编号:");
int no = sc.nextInt();
int pos = -1;
for (int i = 0; i < index; i++) {
if (i == no - 1) {
pos = i;
break;
}
}
if (pos == -1) {
System.out.println("你要插入的位置不存在!");
} else {
for (int i = index; i > pos; i--) {
names[i] = names[i - 1];
prices[i] = prices[i - 1];
}
System.out.print("你要插入的商品名称:");
String name = sc.next();
System.out.print("你要插入的商品价格为:");
double price = sc.nextDouble();
names[pos] = name;
prices[pos] = price;
index++;
nos[index - 1] = index;
}
}
//对商品进行排序
public static void sortProduct() {
double pritemp;
String nametemp;
for (int i = 0; i < index - 1; i++) {
boolean flag = false;
for (int j = 0; j < index - i - 1; j++) {
if (prices[j] > prices[j + 1]) {
pritemp = prices[j];
nametemp = names[j];
prices[j] = prices[j + 1];
names[j] = names[j + 1];
prices[j + 1] = pritemp;
names[j + 1] = nametemp;
flag = true;
}
}
if (!flag) {
break;
}
}
}
}