

package yedongxin.cnblogs.www.base.array;
public class Demo08 {
static void main() {
//1.创建一个二维数组 11*11 0:没有棋子 1:黑棋 2:白棋
int [][] array1 =new int[11][11];
array1[1][2] = 1;
array1[2][3] = 2;
//输出原始数组
System.out.println();
for (int[] ints : array1) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
}
}
/*输出结果
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
*/
package yedongxin.cnblogs.www.base.array;
import org.w3c.dom.ls.LSOutput;
public class array {
static void main() {
//创建二维数组
int[][] chessArr = new int[11][11];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
System.out.println(chessArr.length);
System.out.println(chessArr[0].length);
print(chessArr);
//转换为稀疏数组
int sum = 0;
for (int[] ints : chessArr) {
for (int anInt : ints) {
if(anInt != 0){
sum++;
}
}
}
//创建稀疏数组并赋值
int[][] a = new int[sum+1][3];
a[0][0] = 11;
a[0][1] = 11;
a[0][2] = sum;
int count = 1;
for (int i = 0; i < chessArr.length; i++) {
for (int j = 0; j <chessArr[i].length ; j++) {
if(chessArr[i][j] != 0){
a[count][0] = i;
a[count][1] = j;
a[count][2] = chessArr[i][j];
count++;
}
}
}
//输出稀疏数组
System.out.println("生成的稀疏数组为:");
for (int[] ints : a) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
}
//输出二维数组
public static void print(int[][] array){
for (int i = 0; i < array.length-1; i++) {
for (int j = 0; j < array[i].length-1; j++) {
System.out.print(array[i][j]+"\t");
}
System.out.println();
}
}
}
浙公网安备 33010602011771号