I am using following simple code for 2d array in golang, where APPEND function is resulting in Duplicate values rather than appending.
package main
import "fmt"
func main() {
var n int
fmt.Scanf("%d", &n)
array := [][]int{}
row := make([]int, n)
for _, _ = range row {
for j, _ := range row {
fmt.Scanf("%d", &row[j])
}
fmt.Println("Printing current Row", row)
array = append(array, row)
fmt.Println("Printing curent Array", array)
}
fmt.Println("Final Array", array)
}
But Strangely this are not going unexpectedly. If suppose i want this thing to happen(input)
2
1 2
3 4
and i run this program i get this in return
2 //Dimension for matrix
1 //Iteration one begins
2
Printing current Row [1 2]
Printing curent Array [[1 2]]
3 //Iteration two begins
4
Printing current Row [3 4]
Printing curent Array [[3 4] [3 4]]
Final Array [[3 4] [3 4]]
I am not getting reason that why APPEND function is resulting in duplicating entries . There by want to know how to correct this also underlying CONCEPT
use new slice for each iteration of the first loop, and check for the errors:
you read new data to the old same slice, this is why you have duplicated data.
if you want to use append
first create array with zero length and cap n
:array := make([][]int, 0, n)
then change your first loop to: for i := 0; i < n; i++ {
and inside this loop make new slice: row := make([]int, n)
2 way:
using append (without fmt.Scan
error checking):
package main
import "fmt"
func main() {
n := 0
fmt.Scan(&n)
slice := make([][]int, 0, n)
for i := 0; i < n; i++ {
row := make([]int, n)
for j, _ := range row {
fmt.Scan(&row[j])
}
slice = append(slice, row)
}
fmt.Println(slice)
}
without using append read to s[i][j]
(with error checking):
package main
import "fmt"
func main() {
var n int
if m, err := fmt.Scan(&n); m != 1 {
panic(err)
}
s := make([][]int, n)
for i := 0; i < n; i++ {
s[i] = make([]int, n)
for j := 0; j < n; j++ {
if m, err := fmt.Scan(&s[i][j]); m != 1 {
panic(err)
}
}
}
fmt.Println(s)
}
input:
2
1 2
3 4
output:
[[1 2] [3 4]]
I think this test sample code is clear enough to
show how slice of slice works (with commented output):
package main
import "fmt"
func main() {
s1 := [][]int{}
s2 := []int{1, 2}
s1 = append(s1, s2)
fmt.Println(s1) // [[1 2]]
s2[0], s2[1] = 3, 4
fmt.Println(s1) // [[3 4]]
s1 = append(s1, s2)
fmt.Println(s1) // [[3 4] [3 4]]
s2[0], s2[1] = 30, 40
fmt.Println(s1) // [[30 40] [30 40]]
fmt.Println(len(s2), cap(s2)) // 2 2
s3 := [][]int{
[]int{1, 2},
[]int{3, 4},
s2,
s2,
}
fmt.Println(s3) // [[1 2] [3 4] [30 40] [30 40]]
s2[0] = 100
fmt.Println(s3) // [[1 2] [3 4] [100 40] [100 40]]
}
you created slice of slice s1
like this :
array [0] = row[0], row[1]
array [1] = row[0], row[1]
so when you change row it will seen twice.
This behaviour is occurring because you are not adding a new row
slice to your array
. Each iteration simply reads into the same row
slice overriding previous values. Try below:
func main() {
var n int
fmt.Scanf("%d", &n)
array := [][]int{}
for i := 0; i < n; i++ {
row := make([]int, n) // create a new slice to add next row values
for j, _ := range row {
fmt.Scanf("%d", &row[j])
}
fmt.Println("Printing current Row", row)
array = append(array, row)
fmt.Println("Printing curent Array", array)
}
fmt.Println("Final Array", array)
}