Resizing an array
I have declared what I think is an array of integers
int[] validCoords = new int[1];
I believe this is a 1d array with 1 entry.
How would I resize this?18 Replies
(resizing after the instantiation) I basically want to expand it (and clear it) as I go
The best way would be to just use a list
I'll look into instantiating a list 😄
Would this create an empty list? (named validCoords)
No
You need to specify the type
You don't declare an array with
[] foo = new [1]
?
No
the type of elements goes inside the brackets
<int>
Oh, thanks
also
Array.Resize
I was trying this but couldn't understand the error
no it's a static method
Array.Resize(ref arr, size)
Ohh, thanks man
List just resizes the array for you ¯\_(ツ)_/¯
To each their own, though. Some people like busywork ig
I'm just trying to learn my man haha
Very new to this as you can probably tell
Didn't really know the difference between a list and an Array
Ah, gotcha
Thought it's some misguided attempt at some microoptimization lol
The difference is simple: lists are dynamically-sized
You can add elements, you can remove elements
And you never have to worry about resizing it manually
So lists are used for exactly this usecase haha
Yep
Cheers