首页 > 代码库 > 二维数组的运用
二维数组的运用
二维数组的简单运用
仅供参考
代码如下:
package ClassDemo;
import java.util.Arrays;
import java.util.Scanner;
public class TwoDimensionalArray {
public static void main(String[] args) {
// testDemo1();
int[][] arrayDemo = new int[2][2];
// 1.使用scanner输入值初始化数组
// Scanner sc = new Scanner(System.in);
// for (int row = 0; row < arrayDemo.length; row++) {
// for (int column = 0; column < arrayDemo[row].length; column++) {
// System.out.printf("Enter the element (%d, %d)", row, column);
// arrayDemo[row][column] = sc.nextInt();
// }
// }
// sc.close();
// 2.使用随机数初始化数组
for (int row = 0; row < arrayDemo.length; row++) {
for (int column = 0; column < arrayDemo[row].length; column++) {
arrayDemo[row][column] = (int) (Math.random() * 100);
}
}
// 3.打印数组
for (int row = 0; row < arrayDemo.length; row++) {
for (int column = 0; column < arrayDemo[row].length; column++) {
System.out.print(arrayDemo[row][column] + " ");
}
System.out.print("\n");
}
// 4.对所有元素求和
int sum = 0;
for (int row = 0; row < arrayDemo.length; row++) {
for (int column = 0; column < arrayDemo[row].length; column++) {
sum += arrayDemo[row][column];
}
}
System.out.println("the sum is: " + sum);
// 5(1).对数组按照行求和
int[] sumOfRows = new int[arrayDemo.length];
for (int row = 0; row < arrayDemo.length; row++) {
sumOfRows[row] = 0;
for (int column = 0; column < arrayDemo[row].length; column++) {
sumOfRows[row] += arrayDemo[row][column];
}
}
System.out.println("the sum of rows is: ");
for (int row = 0; row < sumOfRows.length; row++) {
System.out.println(sumOfRows[row]);
}
// 5(2).对数组按照列求和
int[] sumOfColumns = new int[arrayDemo.length];
for (int column = 0; column < arrayDemo[0].length; column++) {
for(int row = 0; row < arrayDemo.length; row++)
sumOfColumns[column] += arrayDemo[row][column];
}
System.out.println("the sum of columns is: " + Arrays.toString(sumOfColumns));
// 6.哪一行的和最大
int maxRowIndex = 0;
int maxRow = sumOfRows[0];
for (int i = 1; i < sumOfRows.length; i++) {
if (maxRow < sumOfRows[i]) {
maxRowIndex = i;
maxRow = sumOfRows[i];
}
}
System.out.println("the max is: " + maxRow + "at row: " + maxRowIndex);
// 7.随意打乱
for (int row = 0; row < arrayDemo.length; row++) {
for (int column = 0; column < arrayDemo[row].length; column++) {
int rowSwap = (int) (Math.random() * arrayDemo.length);
int columnSwap = (int) (Math.random() * arrayDemo[row].length);
//交换arrayDemo[rowSwap][columnSwap]和arrayDemo[row][column]
int temp = arrayDemo[rowSwap][columnSwap];
arrayDemo[rowSwap][columnSwap] = arrayDemo[row][column];
arrayDemo[row][column] = temp;
}
}
//Print the shuffled array
for (int row = 0; row < arrayDemo.length; row++)
System.out.println(Arrays.toString(arrayDemo[row]));
}
private static void testDemo1() {
int[][] i = new int[2][3];
System.out.println("Is i an int[][]? " + (i instanceof int[][]));
System.out.println("Is i[0] an int[]? " + (i[0] instanceof int[]));
//二维变长数组
int[][] a = new int[3][];
a[0] = new int[2];
a[1] = new int[3];
a[2] = new int[1];
for (int j = 0; j < a.length; j++)
System.out.println(Arrays.toString(a[j]));
//以下代码错误: 不能空缺第一维大小
//int[][] b = new int[][3];
int[][] c = new int[][]{{1, 2, 3},{4},{5, 6, 7, 8}};
for(int k = 0; k < c.length; ++k)
{
for(int j = 0; j < c[k].length; ++j)
{
System.out.print(c[k][j]+" ");
}
System.out.println();
}
}
}
二维数组的运用