package main

import (
"fmt"
"reflect"
)

//展示矩阵
func ShowMatrix(arg interface{}) {

value := reflect.ValueOf(arg) //反射获得值
switch value.Kind() {//变量的类型
case reflect.Slice, reflect.Array:
rows := value.Len()
cols := value.Index(0).Len()
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
fmt.Printf("%v", value.Index(i).Index(j))
}
fmt.Println()
}
case reflect.Int:
fmt.Println(value)
fmt.Println("数字")
}
}

//生成稀疏数组
func ToSparse(arr [9][9]int) [][3]int {
dataCap := 0
spareArr := make([][3]int, 1) //切片设计一个容量,下一个步骤才好赋值
spareArr[0] = [3]int{9, 9, 0}
//spareArr = append(spareArr,[3]int{9, 9, 0}) //append 自动给切片扩容
for i, v := range arr {
for k, vv := range v {
if vv != 0 {
dataCap++
spareArr = append(spareArr, [3]int{i, k, vv})
}
}
}
spareArr[0][2] = dataCap
return spareArr
}

//恢复原始数组
func ToOrange(spareArr [][3]int) [9][9]int {
arr1 := [9][9]int{}
//恢复原始数组
for i, v := range spareArr {
if i != 0 {
arr1[v[0]][v[1]] = v[2]
}
}
return arr1
}

func main() {
param1 := [9][9]int{}
param1[1][4] = 1
param1[4][2] = 2
ShowMatrix(param1)
//稀疏数组
res := ToSparse(param1)
fmt.Println(res)
////原始数组
res1 := ToOrange(res)
fmt.Println(res1)

}