- Home /
Adding elements to a 2d array.
I'm working on a game that will use an 2d array to store data. I need the array be dynamic as it will grow as the player plays the game.
I am planning on using the built in 2d arrays (var x = new int[];) because there faster and easier to use.
I need to add additional elements to the beginning and end of the array as the player goes through the game.
How would I go about doing this? Or if someone has a better idea to my problem that would be helpful.
This is for JavaScript btw
How about using dynamic lists ins$$anonymous$$d? Resizing arrays is impossible, so you may as well use a data type that has that kind of functionality built in already.
Answer by Rod-Green · Nov 11, 2011 at 09:02 AM
As mentioned you can create a List type like so:
List<myType> myList = new List<myType>();
And to the add to this list all you need to do is:
myList.Add(myObj); // where myObj is of type myType
To insert at the beginning do:
myList.Insert(0, myObj);
looks interesting, however your example does not seem to be 2 Dimensional. How would you implement it to be 2 Dimensional? From what it looks like you would need to make it into a Jagged array that would make adding and verifying elements a lot more difficult than the simple built in system. Also from what I read this does not have any better performance than the standard JavaScript Array's....
I didn't see this was for JS specifically or for a 2D array. However what concerns do you have about using a jagged array? Seems like you wouldn't have much choice considering you want to add and insert to it.
You could rebuild the 2d array every time but that would get expensive if you do it a lot/large arrays.
So really you can just replace
List<myType> myList
With
List<List<myType>> my2DList
And add to it the same as before
my2DList.Add(myObjList); // where myObjList is of type List<myType>
If you know that the y size of the array is fixed then it could be
List<myType[]> my2DList
Also note 2D lists and jaggad arrays don't serialize(save)
I wanted to use a real 2d array because it represents a 2d grid, would make it simpler to implement. The Array will be used to keep track of a randomly generated game world. $$anonymous$$y intention is to have the world generated as the player explores and in turn resize the array when it gets bigger than its current size.
The point being made here, is Arrays don't resize. If you want to change the size of an array, it basically comes down to creating a new, bigger array, copying all the elements of the old array over into it, and deleting the old array. $$anonymous$$uch simpler to use data types which are built to be dynamically sized.
Syclamoth is correct. You can't have both.
However why don't you just store the random seed used to generate the world and in turn any subsequent plays of the game would regenerate the same. This is NOT how $$anonymous$$ecraft works. ;) So all you'd need to do is store the differences or changes the player made and not the world itself.
Your answer
Follow this Question
Related Questions
check occurence of int in 2darray c# 3 Answers
2D array generated with custom inspector, null when starting game? 1 Answer
Tracking ojects grid positon 1 Answer
Array Within Index 1 Answer
Mouse Position to 2D array 1 Answer