Skip to content

Commit feae248

Browse files
committed
Initial commit
0 parents  commit feae248

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Project exclude paths
2+
/out/

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DataStructures.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.CodeRoot.SparseArray;
2+
3+
public class sparseArr {
4+
/**
5+
* 稀疏数组
6+
*/
7+
public static void main(String[] args){
8+
/**
9+
* 创建一个原始二维数组 11*11
10+
* 0:表示没有棋子
11+
* 1:黑子
12+
* 2:白子
13+
*
14+
* [row][col]
15+
* (1)创建原始数组,并设置好棋子
16+
* (2)遍历一遍看看原始数组,顺便统计有效棋子的个数
17+
* (3)根据有效棋子个数,创建稀疏数组
18+
* (4)将二维数组转换成稀疏数组
19+
*
20+
*/
21+
int chessArr[][]=new int[11][11];
22+
chessArr[1][2]=1;
23+
chessArr[2][3]=2;
24+
chessArr[3][4]=1;
25+
System.out.println("原始二维数组");
26+
27+
int sum=0;//记录 有效数字的个数
28+
for(int[] row:chessArr){
29+
for(int data:row){
30+
System.out.printf("%d\t",data);//这里用格式化输出 printf
31+
if(data!=0){
32+
sum++;//统计 有效数字的个数
33+
}
34+
}
35+
System.out.println("\n");
36+
}
37+
38+
System.out.println("sum = "+ sum);//稀疏数组的行 数等于sum加1
39+
//创建对应的稀疏数组
40+
int sparseArr[][]=new int[sum+1][3];//稀疏数组的列 始终为3
41+
sparseArr[0][0]=11;
42+
sparseArr[0][1]=11;
43+
sparseArr[0][2]=sum;
44+
45+
//二维数组转换成稀疏数组
46+
int temp=1;
47+
for (int i = 0; i < 11; i++) {
48+
for (int j = 0; j <11 ; j++) {
49+
if(chessArr[i][j]!=0){
50+
sparseArr[temp][0]=i;
51+
sparseArr[temp][1]=j;
52+
sparseArr[temp][2]=chessArr[i][j];
53+
temp++;
54+
}
55+
}
56+
}
57+
58+
System.out.println("下面是稀疏数组");
59+
for(int[] row:sparseArr){
60+
for(int data:row){
61+
System.out.printf("%d\t",data);//这里用格式化输出 printf
62+
}
63+
System.out.println("\n");
64+
}
65+
66+
//----------------下面是稀疏数组转二维数组---------------------
67+
/**
68+
* (1)创建新的二维数组
69+
* (2)一层循环遍历稀疏数组(因为稀疏数组的列是永远固定的,不需要遍历列
70+
* (3)遍历时,找到棋子在二维数组上对应的位置,并赋值(黑白棋)
71+
* (4)遍历输出新的二维数组
72+
*/
73+
74+
int chessArr2[][]=new int[ sparseArr[0][0] ] [ sparseArr[0][1] ];//创建新的二维数组
75+
for (int i = 1; i < sum+1; i++) {//遍历 稀疏数组
76+
chessArr2[ sparseArr[i][0] ] [ sparseArr[i][1] ]=sparseArr[i][2];//三合一,将行、列、值全用上
77+
}
78+
79+
System.out.println("下面是稀疏数组转换成的二维数组");
80+
for (int[] row:chessArr2) {
81+
for (int data:row) {
82+
System.out.printf("%d\t",data);
83+
}
84+
System.out.println("\n");
85+
}
86+
87+
88+
89+
}
90+
91+
}

0 commit comments

Comments
 (0)