数据结构包括:线性结构和非线性结构。
找到有用数据的个数,定义稀疏数组的第一行
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;
}
}