✅ 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
Angius3y ago
Array is a reference type
✿ Scarlet ✿
✿ Scarlet ✿OP3y ago
ahbn
Angius
Angius3y ago
You're filling the columns with 4 references to the same lines array
✿ Scarlet ✿
✿ Scarlet ✿OP3y ago
blobSpoop hey how to enumerable repeat by catbruh
Servator
Servator3y ago
What do you want to do ?
Angius
Angius3y ago
I guess you could try a LINQ trick... maybe
✿ Scarlet ✿
✿ Scarlet ✿OP3y ago
im in caffeine overload, little bugged brain
Angius
Angius3y ago
Enumerable.Repeat(0, 4).Select(_ => lines.ToArray()).ToList();
Enumerable.Repeat(0, 4).Select(_ => lines.ToArray()).ToList();
✿ Scarlet ✿
✿ Scarlet ✿OP3y ago
but Hmmmm thanks
Angius
Angius3y ago
arr.ToArray() should create a copy
✿ Scarlet ✿
✿ Scarlet ✿OP3y 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
MODiX3y 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
MODiX3y 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
Angius3y ago
@Atulin Huh, that .Chunk() solution is smart
✿ Scarlet ✿
✿ Scarlet ✿OP3y ago
thanks
Servator
Servator3y ago
You can close thread with /close 👍
✿ Scarlet ✿
✿ Scarlet ✿OP3y ago
\close
Servator
Servator3y ago
not \ with /
✿ Scarlet ✿
✿ Scarlet ✿OP3y ago
waht a fuck
Angius
Angius3y ago
huh Ah, wrong bot's command
Angius
Angius3y ago
Pick the Accord one
✿ Scarlet ✿
✿ Scarlet ✿OP3y ago
i did but i still can post 🧐
Servator
Servator3y ago
Try again
✿ Scarlet ✿
✿ Scarlet ✿OP3y ago
✿ Scarlet ✿
✿ Scarlet ✿OP3y ago
catgun

Did you find this page helpful?