最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论