數據結構包括:線性結構和非線性結構。
找到有用數據的個數,定義稀疏數組的第一行
1線性結構
1) 線性結構作爲最常用的數據結構,其特點是數據元素之間存在一對一的線性關係
2) 線性結構有兩種不同的存儲結構,即順序存儲結構(數組)和鏈式存儲結構(鏈表)。順序存儲的線性表稱爲順序
表,順序表中的存儲元素是連續的。
3) 鏈式存儲的線性表稱爲鏈表,鏈表中的存儲元素不一定是連續的,元素節點中存放數據元素以及相鄰元素的地
址信息
4) 線性結構常見的有:數組、隊列、鏈表和棧,後面我們會詳細講解.
2非線性結構
1.非線性結構包括:二維數組,多維數組,廣義表,樹結構,圖結構
一.稀疏數組
原始數組--------------
0 0 0 0 0 0 0 0 0 0 0 原始數組是 11*11 的有很多的無用數據 稀疏數組意義在於將普通數組壓縮
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 2 0 0 0 0
0 0 0 0 0 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 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
稀疏數組----------- 稀疏的列數 爲有用數據的 值+1 行數固定爲 3
11 11 4 第一行: 記錄原始數組的 大小 11*11 有四個有用的數據
1 2 1 第一行以下: 在原始數組的 1+1行 2+1列 存在一個數爲 1
2 3 2 在原始數組的 2+1行 3+1列 存在一個數爲 2 ...以此類推
3 6 2
5 7 1
原始數組轉換爲稀疏數組方法:
1.確定第一行 2.先定義一個 計數記錄 稀疏數組的行數是第幾行了
3.循環原始數組 將不爲0的數 的行列座標定義爲 稀疏數組的 值 4.每遇到一個不爲0的 count++ 即移到下一行
private static int[][] CreateSparseArray(int arrayOne[][],int num ) {
定義一個稀疏數組
int sparseArray[][] = new int[num+1][3];
定義稀疏數組的第一行,即數組是幾*幾的,有效數據有幾個
sparseArray[0][0]=11;
sparseArray[0][1]=11;
sparseArray[0][2]=num;
int count = 1;
for (int i = 0; i < arrayOne.length; i++) {
for (int j = 0; j < arrayOne.length; j++) {
if (arrayOne[i][j]!=0){
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = arrayOne[i][j];
count++;
}
}
}
return sparseArray;}
稀疏數組轉換爲原始數組方法:
1.原始數組的 行爲稀疏數組的 0 行 0 列 2.1.原始數組的 行爲稀疏數組的 0 行 1 列
3.循環稀疏數組 從 第2行開始(因爲第一行 已經使用) 3.取出原始數組有值的行列 稀疏數組的第三列 爲它的值
//還原數組
int rows = sparseArrayFromFile[0][0];
int cloums = sparseArrayFromFile[0][1];
int[][] arrayTwo = new int[rows][cloums];
for (int i = 1; i < sparseArrayFromFile.length; i++) {
for (int j = 0; j <3; j++) {
int row = sparseArrayFromFile[i][0];
int cloum = sparseArrayFromFile[i][1];
int value = sparseArrayFromFile[i][2];
arrayTwo[row][cloum]=value;
}
}
完整代碼
package dataStructure.稀疏數組;
import java.io.*;
public class test1 {
public static void main(String[] args) throws Exception {
//創建普通數組
int[][] arrayOne = createArray(11,11);
//循環出該數組的有用數據有幾個
int num = getUsefuldata(arrayOne);
//得到稀疏數組
int sparseArray[][] = CreateSparseArray(arrayOne,num);
System.out.println("原始數組--------------");
priintArray(arrayOne);
// priintArray(sparseArray);
//將稀疏數組寫入本地文件
saveData("map.data", sparseArray);
//讀取文件得到稀疏數組
int sparseArrayFromFile[][] = readFile("map.data");
System.out.println();
System.out.println("讀取文件得到稀疏數組-----------");
priintArray(sparseArrayFromFile);
//還原數組
int rows = sparseArrayFromFile[0][0];
int cloums = sparseArrayFromFile[0][1];
int[][] arrayTwo = new int[rows][cloums];
for (int i = 1; i < sparseArrayFromFile.length; i++) {
for (int j = 0; j <3; j++) {
int row = sparseArrayFromFile[i][0];
int cloum = sparseArrayFromFile[i][1];
int value = sparseArrayFromFile[i][2];
arrayTwo[row][cloum]=value;
}
}
System.out.println("還原後--------");
priintArray(arrayTwo);
}
private static int[][] readFile(String s) throws Exception {
FileReader fileReader = new FileReader(s);
BufferedReader bufferedReader = new BufferedReader(fileReader);
//定義數組
int sparseArray[][];
//總的有效數據個數
int rows;
String line;
if ((line=bufferedReader.readLine())!=null){
String[] rowone = line.split(",");
rows = Integer.parseInt(rowone[2]);
sparseArray=new int [rows+1][3];
sparseArray[0][0]=Integer.parseInt(rowone[0]);
sparseArray[0][1]=Integer.parseInt(rowone[1]);
sparseArray[0][2]=rows;
}else {
return null;
}
String lines ;
//設置稀疏數組 第幾行的標誌
int conut = 1;
while ((lines=bufferedReader.readLine())!=null){
String[] split = lines.split(",");
sparseArray[conut][0]=Integer.parseInt(split[0]);
sparseArray[conut][1]=Integer.parseInt(split[1]);
sparseArray[conut][2]=Integer.parseInt(split[2]);
conut++;
}
fileReader.close();
return sparseArray;
}
private static void saveData(String s, int[][] sparseArray) throws Exception {
File file = new File(s);
FileOutputStream fileOutputStream = new FileOutputStream(file);
for (int i = 0; i < sparseArray.length; i++) {
for (int j = 0; j < 3; j++) {
fileOutputStream.write((sparseArray[i][j]+"").getBytes());
if (j!=2){
fileOutputStream.write(",".getBytes());
}
}
fileOutputStream.write("\n".getBytes());
}
fileOutputStream.close();
}
private static void priintArray(int[][] Array ) {
for (int[] ints : Array) {
System.out.println();
for (int anInt : ints) {
System.out.print("\t");
System.out.print(anInt);
}
}
}
private static int[][] CreateSparseArray(int arrayOne[][],int num ) {
//定義一個稀疏數組
int sparseArray[][] = new int[num+1][3];
//定義稀疏數組的第一行,即數組是幾*幾的,有效數據有幾個
sparseArray[0][0]=11;
sparseArray[0][1]=11;
sparseArray[0][2]=num;
int count = 1;
for (int i = 0; i < arrayOne.length; i++) {
for (int j = 0; j < arrayOne.length; j++) {
if (arrayOne[i][j]!=0){
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = arrayOne[i][j];
count++;
}
}
}
return sparseArray;
}
private static int[][] createArray(int a,int b) {
//創建一個11*11的二維數組
int[][] arrayOne = new int[11][11];
//設置兩個棋子 黑子表示1 白子表示2
arrayOne[1][2] = 1;
arrayOne[2][3] = 2;
arrayOne[3][6] = 2;
arrayOne[5][7] = 1;
return arrayOne;
}
private static int getUsefuldata(int[][] arrayOne) {
int num =0;
for (int i = 0; i < arrayOne.length; i++) {
for (int j = 0; j < arrayOne.length; j++) {
if (arrayOne[i][j]!=0){
num++;
}
}
}
return num;
}
}