JavaScript arrays

BlitzMax Forums/BlitzMax Programming/JavaScript arrays

verfum(Posted 2008) [#1]
Hi, I've been trying to convert some javascript to Bmax but I can't figure out what arrays are in Java? I.e. are they blitz arrays or lists, I'm a bit confused because Java arrays seem to be both!! Here is some code:

var array = new Array();
var array2 = new Array();

var a = 0;

array[a] = false;

var length = array2.length;


That's obviously just an example, but I think you know what I mean.


Azathoth(Posted 2008) [#2]
Java and JavaScript are two different languages.


Dreamora(Posted 2008) [#3]
Java arrays are just a container class, as is BMs Array class.

How you port your code depends on what it is meant to do. In above example you surely would use arrays.


verfum(Posted 2008) [#4]
It's JavaScript.

Yeah see I thought it would be arrays too but arrays dont allow you to AddLast getLength etc. Do they?


Dreamora(Posted 2008) [#5]
???

somearray.length is its length
but you are right, addlast etc does not work like that, thats why slicing exists.


nino(Posted 2008) [#6]
JS arrays are not real arrays like in c/c++.
They are array like objects.

http://www.w3schools.com/jsref/jsref_obj_array.asp


verfum(Posted 2008) [#7]
They seem very similar to Lists, but the added ability to do this, which is annoying
array[myVariable];



nino(Posted 2008) [#8]
If you need a list value at a certain index use ValueAtIndex

Global t:TList=New TList
t.addLast("ab")
t.addLast("cd")
Print String(t.ValueAtIndex(1))



Czar Flavius(Posted 2008) [#9]
Javascript arrays are NOT linked lists. In terms of use they are like normal arrays but with the feature that they can increase in size dynamically to what is required, and they can hold different types of variable.

For example, [0] could be integer and [1] could be string.

If you created an array of size 5, but then made [9] = something, the array would increase in size to be 10 elements long. ([5]-[8] being empty for now). This can be both very useful and annoying depending upon what you are doing, and your ideas of what is "good practice".