当前位置:网站首页>稀疏数组→五子棋的存盘续盘等操作
稀疏数组→五子棋的存盘续盘等操作
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();
}
}
}
运行结果:
边栏推荐
- Basic operation of vim
- 34. Analyze flexible.js
- 素数筛选(埃氏筛法,区间筛法,欧拉筛法)
- Advantages of smart exhibition hall design and applicable industry analysis
- Use of collection framework
- 抽卡程序模拟
- 1、 MySQL Foundation
- Gradio quickly builds ml/dl Web Services
- Interface and abstract class / method learning demo
- Dialog introduction
猜你喜欢

JVM上篇:内存与垃圾回收篇十二--StringTable

一、MySQL基础

Explore the mysteries of the security, intelligence and performance of the universal altek platform!

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

1、 MySQL Foundation

Why is count (*) slow

Li Kou achieved the second largest result

整合SSM

Scientific Computing Library - numpy

树莓派输出PWM波驱动舵机
随机推荐
Create datasource using Druid connection pool
Select user stories | the false positive rate of hole state in jushuitan is almost 0. How to do this?
Could not autowire. No beans of ‘userMapper‘ type found.
Detailed explanation of pointer constant and constant pointer
Three paradigms, constraints, some keyword differences,
Quoted popular explanation
35. Scroll
Set static IP for raspberry pie
JVM Part 1: memory and garbage collection part 9 - runtime data area - object instantiation, memory layout and access location
Could not autowire.No beans of ‘userMapper‘ type found.
Scientific Computing Library -- Matplotlib
Detailed description of binary search tree
Sub database and sub table
JVM Part 1: memory and garbage collection part 14 -- garbage collector
一、MySQL基础
Sunyanfang, co-founder of WeiMiao: take compliance as the first essence and become the "regular army" of financial and business education
[acwing] solution to the 61st weekly match
Solution and principle analysis of feign call missing request header
标准对话框 QMessageBox
The project connects with Alipay payment, and the intranet penetration realizes the monitoring of asynchronous callback notification of successful payment of Alipay