✅ Help in 2d matrix

var lines = Enumerable.Repeat(1, 4).ToArray();
var columns = Enumerable.Repeat(lines, 4).ToList();
columns[0][0] = 2;
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(columns));

//[[2,1,1,1],[2,1,1,1],[2,1,1,1],[2,1,1,1]]
var lines = Enumerable.Repeat(1, 4).ToArray();
var columns = Enumerable.Repeat(lines, 4).ToList();
columns[0][0] = 2;
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(columns));

//[[2,1,1,1],[2,1,1,1],[2,1,1,1],[2,1,1,1]]
Why does it changes all the inner arrays instead of the first one kekw
25 Replies
Angius
Angius2y ago
Array is a reference type
✿ Scarlet ✿
✿ Scarlet ✿OP2y ago
ahbn
Angius
Angius2y ago
You're filling the columns with 4 references to the same lines array
✿ Scarlet ✿
✿ Scarlet ✿OP2y ago
blobSpoop hey how to enumerable repeat by catbruh
Servator
Servator2y ago
What do you want to do ?
Angius
Angius2y ago
I guess you could try a LINQ trick... maybe
✿ Scarlet ✿
✿ Scarlet ✿OP2y ago
im in caffeine overload, little bugged brain
Angius
Angius2y ago
Enumerable.Repeat(0, 4).Select(_ => lines.ToArray()).ToList();
Enumerable.Repeat(0, 4).Select(_ => lines.ToArray()).ToList();
✿ Scarlet ✿
✿ Scarlet ✿OP2y ago
but Hmmmm thanks
Angius
Angius2y ago
arr.ToArray() should create a copy
✿ Scarlet ✿
✿ Scarlet ✿OP2y ago
jusst toarray didnt work ill try this select one oh your solution did work! thanks whats your gh @? ill lik you as co author
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
var a1 = new int[]{1, 2, 3};
var a2 = Enumerable
.Repeat(0, 4)
.Select(_ => a1.ToArray())
.ToList();

a2[1] = new int[]{4, 5, 6};

a2
var a1 = new int[]{1, 2, 3};
var a2 = Enumerable
.Repeat(0, 4)
.Select(_ => a1.ToArray())
.ToList();

a2[1] = new int[]{4, 5, 6};

a2
Result: List<int[]>
[
[
1,
2,
3
],
[
4,
5,
6
],
[
1,
2,
3
],
[
1,
2,
3
]
]
[
[
1,
2,
3
],
[
4,
5,
6
],
[
1,
2,
3
],
[
1,
2,
3
]
]
Compile: 571.554ms | Execution: 83.665ms | React with ❌ to remove this embed.
MODiX
MODiX2y ago
Samarichitane#3472
REPL Result: Success
var board = Enumerable.Repeat('x', 64).Chunk(8);

board
var board = Enumerable.Repeat('x', 64).Chunk(8);

board
Result: <ChunkIterator>d__69<char>
[
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
]
]
[
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
],
[
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x"
]
]
Compile: 531.205ms | Execution: 34.442ms | React with ❌ to remove this embed.
Angius
Angius2y ago
@Atulin Huh, that .Chunk() solution is smart
✿ Scarlet ✿
✿ Scarlet ✿OP2y ago
thanks
Servator
Servator2y ago
You can close thread with /close 👍
✿ Scarlet ✿
✿ Scarlet ✿OP2y ago
\close
Servator
Servator2y ago
not \ with /
✿ Scarlet ✿
✿ Scarlet ✿OP2y ago
waht a fuck
Angius
Angius2y ago
huh Ah, wrong bot's command
Angius
Angius2y ago
Pick the Accord one
✿ Scarlet ✿
✿ Scarlet ✿OP2y ago
i did but i still can post 🧐
Servator
Servator2y ago
Try again
✿ Scarlet ✿
✿ Scarlet ✿OP2y ago
✿ Scarlet ✿
✿ Scarlet ✿OP2y ago
catgun
Want results from more Discord servers?
Add your server