1

I am writing a game for the Windows App store that will use HTML5 and Javascript.

Are there any implementations of ArrayList or LinkedList for this platform? If so could someone give me an example how to use it?

Daniel
  • 10,864
  • 22
  • 84
  • 115
goose
  • 47
  • 1
  • 8
  • are you looking for 1-1 correspondence with the .NET classes? You probably won't find that, but the JavaScript Array should suffice and you can find a few sample JavaScript classes implementing a linked-list structure. Both are data structures that can be built without too much trouble in JavaScript (and most languages) – Jim O'Neil Jan 28 '13 at 03:55
  • I know we can implement a linked-list structure in JS. But my problem is release memory. JS has garbage collection but if I use an Array or implement linked-list, it does not auto release memory. How can I solve this problem? – goose Jan 30 '13 at 02:19
  • @goose: Of course JS garbage collection also works with arrays. Why don't you think so? – Bergi Feb 03 '13 at 12:36

2 Answers2

1

A JavaScript Array does not have a fixed length and thus can be used like an ArrayList. You just have to use the right methods:

  • arrayList.Add(element) = array.push(element)
    Adds an element to the end of the array. The push/pop naming works like a queue: add/remove at the end.
  • arrayList.AddRange(collection) = array.push(element1, element2, ...)
    Adds multiple elements to the end of the array. Like many other Array methods, push can take a variable number of elements to append.
  • arrayList.RemoveRange(index, count) = array.splice(index, count)
    The name is a bit less obvious. splice removes the given number of elements (count) at index.
  • arrayList.RemoveAt(index) = array.splice(index, 1):
    Passing 1 as count to splice removes just one element.
  • arrayList.Insert(index, x) = array.splice(index, 0, element)
    splice also takes a variable number of elements to insert at the index after removing the given number of elements. By removing no elements (passing 0 as count), you can use it to simply insert new elements.

All of these methods correctly adjust the length of the array and shift elements around, as opposed to delete array[index]. delete simply removes properties from an object and does not treat arrays differently, so you're left with a "gap".

Mattias Buelens
  • 19,609
  • 4
  • 45
  • 51
0

I figured out my problem! All I want that I can release memory in array. It can be done by using "delete" keyword for each element of array, but array's length is not reduced.

And about Array List in JavaScript, I think the current build-in array is fine. No need to implement more.

goose
  • 47
  • 1
  • 8
  • No, you never use `delete` to [remove array items](http://stackoverflow.com/q/500606/1048572) – Bergi Feb 03 '13 at 12:35