Click here to Skip to main content
16,017,907 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hey!
I have created an array that has 50 places.
When the places are full, I want it to constantly expand with 1 place in the array, but it does not work...

I need to use array and can not use List..

And I can not use array.resize...

Please help

What I have tried:

I have a code that look like this (see bellow) but it do not work..

public static int[] ExtendArray (int[] vector, int new)
{
int[] newArray = new int [vector.Lenght + 1];
for (int i = 0; i < vector.Lenght; i++
{
newArray[i] = vector[i];
}
newArray[vector.Lenght] = new;
return newArray.
Posted
Updated 17-Mar-21 5:17am
Comments
Daniel Pfeffer 17-Mar-21 11:08am    
Try spelling Length correctly (not Lenght)
Richard Deeming 17-Mar-21 11:44am    
"Doesn't work" is useless as a problem description. If you can't describe the problem clearly, then you've still got a lot to learn.

Aside from the misspelling of Length, the fact that you can't call a variable / parameter new in C#, and the missing closing bracket and parentheses, what is the actual problem with your code?

 
Share this answer
 
Comments
Richard Deeming 17-Mar-21 11:36am    
Array.Resize[^] would probably be a simpler option. :)
Richard MacCutchan 17-Mar-21 11:40am    
I am not even sure that Array.Copy is allowed given his comments.
The best solution is to not use an array - use a List<int> instead. Internally, it uses an array for storage, but externally it appears as expandable.

This may help as to why your solution is a very bad idea from a performance point of view: List<T> - Is it really as efficient as you probably think?[^]
 
Share this answer
 
Comments
Member 15104493 17-Mar-21 11:27am    
I can not use List because then I do not get any grades...:(
OriginalGriff 17-Mar-21 12:01pm    
So you need to fix it.
It's pretty obvious what the problem is with your code - even without you telling us the error message, which I can predict with pretty good certainty!

But it is really easy to see and fix if you think about it for a minute, and perhaps use the debugger to watch what is happening while your code is running.
Hint: look at the line that the error appears on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900