C
C#•2y ago
alex_aom

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
alex_aom
alex_aomOP•2y ago
(resizing after the instantiation) I basically want to expand it (and clear it) as I go
Angius
Angius•2y ago
The best way would be to just use a list
alex_aom
alex_aomOP•2y ago
I'll look into instantiating a list 😄
List<> validCoords = new List<>();
List<> validCoords = new List<>();
Would this create an empty list? (named validCoords)
Angius
Angius•2y ago
No You need to specify the type You don't declare an array with [] foo = new [1]
alex_aom
alex_aomOP•2y ago
int List<> validCoords = new List<>();
int List<> validCoords = new List<>();
?
Angius
Angius•2y ago
No
Anton
Anton•2y ago
the type of elements goes inside the brackets <int>
alex_aom
alex_aomOP•2y ago
Oh, thanks
Anton
Anton•2y ago
also Array.Resize
alex_aom
alex_aomOP•2y ago
I was trying this but couldn't understand the error
Anton
Anton•2y ago
no it's a static method Array.Resize(ref arr, size)
alex_aom
alex_aomOP•2y ago
Ohh, thanks man
Angius
Angius•2y ago
List just resizes the array for you ¯\_(ツ)_/¯ To each their own, though. Some people like busywork ig
alex_aom
alex_aomOP•2y ago
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
Angius
Angius•2y ago
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
alex_aom
alex_aomOP•2y ago
So lists are used for exactly this usecase haha
Angius
Angius•2y ago
Yep
alex_aom
alex_aomOP•2y ago
Cheers
Want results from more Discord servers?
Add your server