is there a Vector Java class equivalent in javascript? - Stack Overflow

If one were to look at Java class Vector in the official API, there are certain conveniences such as cr

If one were to look at Java class Vector in the official API, there are certain conveniences such as creating a Vector object without specifying its initial length.

One can just add elements to it without having to specify the index (like an array).

One can also use .contains to determine if the Vector collection contains the element, without having to loop.

Is there such a type in Javascript?

If one were to look at Java class Vector in the official API, there are certain conveniences such as creating a Vector object without specifying its initial length.

One can just add elements to it without having to specify the index (like an array).

One can also use .contains to determine if the Vector collection contains the element, without having to loop.

Is there such a type in Javascript?

Share Improve this question asked Oct 23, 2013 at 19:16 LaneLaneLaneLane 3537 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

JavaScript arrays don't have fixed lengths and are essentially equivalent to Vector.

  • You can add an element at any index. The .length property keeps track of the biggest numerically-indexed property.
  • The .indexOf method is like .contains(). (Don't make heavy use of this, either in Java or JavaScript. Version 6 of Ant, for example, had horrible performance problems in large projects because code was written to more-or-less use ArrayList instances like Maps. If you have collections of any size and need to do look-ups frequently, use a better data structure.)
  • To add an element to the end, you can do either:

    someArray.push( newValue );
    

    or:

    someArray[someArray.length] = newValue;
    

You can create Array and not specify its size:

var array = [];

You can push to it w/o index:

array.push(value);

You can check if it contains specific value, like so:

array.indexOf(value); // returns -1 if it didn't find a match

Check out the spec if you need to find more info about arrays in JavaScript (and only about them).

What you're after is basically a simple JS array. You can use push() to add to the array without having to worry about indexes. indexOf() operates like contains().

Have a look at:

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745137902a4613290.html

相关推荐

  • is there a Vector Java class equivalent in javascript? - Stack Overflow

    If one were to look at Java class Vector in the official API, there are certain conveniences such as cr

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信