稀疏数组和二维数组的转化
- 二维数组转化成稀疏数组思路
- 创建一个二维数组
chessArr[11][11]
,放入一些非零元素,并打印输出 - 遍历二维数组,统计非零元素的个数num
- 创建稀疏数组
sparseArr[num + 1][3]
- 遍历二维数组,把非零元素放到稀疏数组中,并打印输出
- 稀疏数组转化成二维数组思路
- 创建二维数组
chessArr[sparseArr[0][0]][sparseArr[0][1]]
- 遍历稀疏数组,把元素放到二维数组里,并打印输出
public class SparseArray {
public static void main(String[] args) {
int chessArr[][] = new int[11][11];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
chessArr[3][4] = 10;
System.out.println("原始的二维数组:");
for (int[] ints : chessArr) {
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
System.out.println();
int num = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr[i][j] != 0){
num++;
}
}
}
int sparseArr[][] = new int[num + 1][3];
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = num;
int count = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr[i][j] != 0){
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr[i][j];
}
}
}
System.out.println("稀疏矩阵的行数:" + sparseArr.length);
System.out.println();
System.out.println("转化的稀疏数组:");
for (int[] ints : sparseArr) {
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
System.out.println();
int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
for (int i = 1; i < sparseArr.length; i++) {
chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
System.out.println("恢复后的二维数组:");
for (int[] ints : chessArr2) {
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
}
}
把稀疏数组打印到文件里并恢复
public class PrintSparseArray {
public static void main(String[] args) throws Exception {
int chessArr[][] = new int[11][11];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
chessArr[3][4] = 10;
int num = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr[i][j] != 0) {
num++;
}
}
}
int sparseArr[][] = new int[num + 1][3];
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = num;
int count = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr[i][j] != 0) {
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr[i][j];
}
}
}
FileOutputStream f = new FileOutputStream("DataStructures\\src\\chess.txt");
for (int i = 0; i < sparseArr.length; i++) {
f.write((sparseArr[i][0] + "\t" + sparseArr[i][1] + "\t" + sparseArr[i][2]).getBytes());
if (i != sparseArr.length - 1){
f.write("\n".getBytes());
}
}
BufferedReader br = new BufferedReader(new FileReader("DataStructures\\src\\chess.txt"));
String line = "";
int lineNum = 0;
int chessArr2[][] = null;
while((line = br.readLine()) != null){
lineNum++;
if (lineNum == 1){
String[] array = line.split("\t");
chessArr2 = new int[Integer.parseInt(array[0])][Integer.parseInt(array[1])];
}else{
String[] array = line.split("\t");
chessArr2[Integer.parseInt(array[0])][Integer.parseInt(array[1])] =
Integer.parseInt(array[2]);
}
}
for (int[] ints : chessArr2) {
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
}
}
数组模拟顺序队列
public class ArrayQueue {
private int maxSize;
private int front;
private int rear;
private int[] array;
public ArrayQueue(int arraySize) {
maxSize = arraySize;
array = new int[maxSize];
front = -1;
rear = -1;
}
public boolean isEmpty(){
return front == rear;
}
public boolean isFull(){
return rear == maxSize - 1;
}
public void EnQueue(int n){
if (isFull()){
System.out.println("队列已满,入队失败");
return;
}
array[++rear] = n;
}
public int DeQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,出队失败");
}
return array[++front];
}
public void showQueue(){
if (isEmpty()){
System.out.println("队列为空");
return;
}
int j = front + 1;
for (int i = 0; i < (rear - front); i++) {
System.out.println("array[" + i + "] = " + array[j]);
j++;
}
}
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空");
}
return array[front + 1];
}
}
public class test {
public static void main(String[] args) {
ArrayQueue queue = new ArrayQueue(3);
Scanner sc = new Scanner(System.in);
boolean loop = true;
char key;
while (loop){
System.out.println("'E'EnQueue:入队");
System.out.println("'D'EeQueue:出队");
System.out.println("'h'headQueue:查看头部元素");
System.out.println("'s'showQueue:查看所有元素");
System.out.println("'e'exit:退出");
key = sc.next().charAt(0);
switch (key){
case 'E':
System.out.println("请输入一个数:");
int value = sc.nextInt();
queue.EnQueue(value);
break;
case 'D':
try {
int n = queue.DeQueue();
System.out.println("取出的元素为:" + n);
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
case 'h':
try {
int n = queue.headQueue();
System.out.println("头部元素为:" + n);
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
case 's':
queue.showQueue();
break;
case 'e':
loop = false;
break;
default:
break;
}
}
}
}
数组模拟循环队列
- front初始值为0,指向队首元素
- rear初始值为0,指向队尾元素的后一个位置
- rear = front时队列为空
- (rear + 1) % maxSize = front时队列满
- 队列中的元素个数:(rear + maxSize - front)% maxSize
public class CircleQueue {
private int maxSize;
private int front;
private int rear;
private int[] array;
public CircleQueue(int arraySize) {
maxSize = arraySize;
array = new int[maxSize];
front = 0;
rear = 0;
}
public boolean isEmpty(){
return front == rear;
}
public boolean isFull(){
return (rear + 1) % maxSize == front;
}
public void EnQueue(int n){
if (isFull()){
System.out.println("队列已满,入队失败");
return;
}
array[rear] = n;
rear = (rear + 1) % maxSize;
}
public int DeQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,出队失败");
}
int n = array[front];
front = (front + 1) % maxSize;
return n;
}
public void showQueue(){
if (isEmpty()){
System.out.println("队列为空");
return;
}
for (int i = front; i < front + size(); i++) {
System.out.println("array[" + i % maxSize + "] = " + array[i % maxSize]);
}
}
public int size(){
return (rear + maxSize - front) % maxSize;
}
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空");
}
return array[front];
}
}
public class test {
public static void main(String[] args) {
CircleQueue queue = new CircleQueue(4);
Scanner sc = new Scanner(System.in);
boolean loop = true;
char key;
while (loop){
System.out.println("'E'EnQueue:入队");
System.out.println("'D'EeQueue:出队");
System.out.println("'h'headQueue:查看头部元素");
System.out.println("'s'showQueue:查看所有元素");
System.out.println("'e'exit:退出");
key = sc.next().charAt(0);
switch (key){
case 'E':
System.out.println("请输入一个数:");
int value = sc.nextInt();
queue.EnQueue(value);
break;
case 'D':
try {
int n = queue.DeQueue();
System.out.println("取出的元素为:" + n);
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
case 'h':
try {
int n = queue.headQueue();
System.out.println("头部元素为:" + n);
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
case 's':
queue.showQueue();
break;
case 'e':
loop = false;
break;
default:
break;
}
}
}
}