当前位置:网站首页>稀疏数组→五子棋的存盘续盘等操作
稀疏数组→五子棋的存盘续盘等操作
2022-07-27 05:01:00 【new一个对象_】
稀疏数组可方便记录五子棋的存盘,续盘等操作
public class WZQ {
public static void main(String[] args) {
//1.创建一个二维数组11*11 0:没有棋子 1:黑棋 2:白棋
int[][] array1 = new int[11][11];
array1[1][2] = 1;
array1[2][3] = 2;
//输出原始数组
System.out.println("输出原始数组");
//array1.for
for (int[] ints : array1) {
//ints.for
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
System.out.println("===========================");
//转换为稀疏数组保存
// 获取有效值的个数
int sum=0;
for(int i=0;i<11;i++)
for(int j=1;j<11;j++) {
if (array1[i][j] != 0) {
sum++;
}
}
System.out.println("有效值的个数为:"+sum);
//2.创建一个稀疏数组的数组
int[][]array2=new int[sum+1][3];
array2[0][0]=11;
array2[0][1]=11;
array2[0][2]=sum;
//遍历稀疏数组,将非零的值,存放在稀疏数组中
int count=0;
for (int i = 0; i< array1.length; i++) {
for (int j= 0; j <array1.length ; j++) {
if(array1[i][j]!=0){
count++;
array2[count][0]=i;
array2[count][1]=j;
array2[count][2]=array1[i][j];
}
}
}
//输出稀疏数组
System.out.println("输出稀疏数组");
for (int i = 0; i < array2.length; i++) {
System.out.println(array2[i][0]+"\t"+array2[i][1]+"\t"+array2[i][2]+"\t");
}
System.out.println("=====================");
System.out.println("还原");
//1.读取稀疏数组
int[][]array3=new int[array2[0][0]][array2[0][1]];
//2.给其中元素还原它的值
for (int i = 1; i <array2.length ; i++) {
array3[array2[i][0]][array2[i][1]]=array2[i][2];
}
//3.打印
System.out.println("还原原始数组");
//array1.for
for (int[] ints : array3) {
//ints.for
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
}
}
运行结果:
边栏推荐
猜你喜欢

TypeScript 详解

JVM上篇:内存与垃圾回收篇十四--垃圾回收器

The project connects with Alipay payment, and the intranet penetration realizes the monitoring of asynchronous callback notification of successful payment of Alipay

Could not autowire. No beans of ‘userMapper‘ type found.

JVM上篇:内存与垃圾回收篇二--类加载子系统

Constraints of MySQL table

How idea creates a groovy project (explain in detail with pictures and texts)

pyside2____ 1. Installation and case listing

The difference between strlen and sizeof

JVM Part 1: memory and garbage collection part 3 - runtime data area - overview and threads
随机推荐
B1027 打印沙漏
Standard dialog qmessagebox
feign调用丢失请求头问题解决及原理分析
节流函数的demo——正则表达式匹配
Slashes / and backslashes involved in writing code\
2021 OWASP top 4: unsafe design
使用ngrok做内网穿透
Bean的生命周期&&依赖注入*依赖自动装配
Quoted popular explanation
B1025 反转链表*******
Detailed explanation of pointer constant and constant pointer
Why is count (*) slow
《Robust and Precise Vehicle Localization based on Multi-sensor Fusionin Diverse City Scenes》翻译
35.滚动 scroll
JVM上篇:内存与垃圾回收篇三--运行时数据区-概述及线程
What is the future development direction of software testing engineers?
JVM Part 1: memory and garbage collection part 14 -- garbage collector
B1028 人口普查
老子云携手福昕鲲鹏,首次实现3D OFD三维版式文档的重大突破
I've heard the most self disciplined sentence: those I can't say are silent