当前位置:网站首页>Sparse array → saving and continuation of Gobang
Sparse array → saving and continuation of Gobang
2022-07-27 05:25:00 【New an object_】
Sparse array can be used to record the storage of Gobang , Continue disk and other operations
public class WZQ {
public static void main(String[] args) {
//1. Create a 2D array 11*11 0: No chess pieces 1: Black chess 2: White chess
int[][] array1 = new int[11][11];
array1[1][2] = 1;
array1[2][3] = 2;
// Output original array
System.out.println(" Output original array ");
//array1.for
for (int[] ints : array1) {
//ints.for
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
System.out.println("===========================");
// Convert to sparse array to save
// Get the number of valid values
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(" The number of valid values is :"+sum);
//2. Create an array of sparse arrays
int[][]array2=new int[sum+1][3];
array2[0][0]=11;
array2[0][1]=11;
array2[0][2]=sum;
// Traversing sparse arrays , Will be a non-zero value , Stored in a sparse array
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];
}
}
}
// Output sparse array
System.out.println(" Output sparse array ");
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(" Restore ");
//1. Read sparse arrays
int[][]array3=new int[array2[0][0]][array2[0][1]];
//2. Restore its value to one of its elements
for (int i = 1; i <array2.length ; i++) {
array3[array2[i][0]][array2[i][1]]=array2[i][2];
}
//3. Print
System.out.println(" Restore the original array ");
//array1.for
for (int[] ints : array3) {
//ints.for
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
}
}
Running results :
边栏推荐
猜你喜欢

JVM part I: memory and garbage collection part II -- class loading subsystem

pyside2____1.安装和案列

Li Kou achieved the second largest result

redis发布订阅模式

李宏毅机器学习组队学习打卡活动day04---深度学习介绍和反向传播机制

如何快速有效解决数据库连接失败问题

Database design - relational data theory (ultra detailed)

Sunyanfang, co-founder of WeiMiao: take compliance as the first essence and become the "regular army" of financial and business education

JVM Part 1: memory and garbage collection part 12 -- stringtable

实用小工具: Kotlin 代码片段
随机推荐
pyside2____1.安装和案列
B1022 D进制的A+B
集合框架的使用
素数筛选(埃氏筛法,区间筛法,欧拉筛法)
redis集群
使用Druid连接池创建DataSource(数据源)
numpy 数据类型转化
如何查看导师的评价
Shell course summary
2022年郑州轻工业新生赛题目-打死我也不说
JVM Part 1: memory and garbage collection part 7 -- runtime data area heap
idea远程调试debug
File processing (IO)
B1023 group minimum
Li Kou achieved the second largest result
35. Scroll
2021 OWASP top 5: security configuration error
Machine learning overview
JVM上篇:内存与垃圾回收篇十一--执行引擎
SQL数据库→约束→设计→多表查询→事务