1. 程式人生 > >二維數組的運用

二維數組的運用

隨機 輸入 system.in demo1 std scan clas util pack

二維數組的簡單運用

僅供參考

代碼如下:

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();
}
}
}

二維數組的運用