JavaScript 배열 API를 소개했습니다. JavaScript에서는 배열이 그 자체로 매우 강력합니다. 모든 유형을 저장할 수 있으며 길이는 자동으로 확장됩니다. 또한 배열에서 트래버스, 필터링 및 기타 작업을위한 방법을 제공합니다.
완전한 Java 배열 (고정 길이, 단일 유형)입니다. Java의 컬렉션 클래스는 어레이의 단점을 보충합니다. 기본 레이어의 대부분은 객체 [] 스토리지를 사용하여 동적 확장 전략 만 제공합니다. 물론 JDK의 풍부한 API는 다른 언어와 일치하기가 어렵습니다.
그러나 그것은 Java와 JavaScript에 대한 나의 사랑을 방해하지 않습니다.
Java는 중년의 늙은 여자와 같습니다. 당신은 항상 JDK에서 그녀의 매력을 볼 수 있습니다. 대규모 분산 시스템을 구축함으로써 그녀는 진지한 가르침을 반영 할 수 있습니다.
JavaScript는 개화하려는 소녀입니다. 그녀가 꽃을 피울 때마다, 그것은 당신의 마음에 잔물결을 일으킬 것입니다. 당신은 당신을 위해 그것을 사용하기 위해 신중하게 훈련을 받아야합니다.
좋아, 부적절한 은유를 용서해주십시오. 실용적인 것들을 알려주세요.
/ ** *@class arraylist *@description *@time 2014-09-16 21:59 *@author Starzou **/ function arraylist (arr) {this._elementData = arr || []; } var arraylistPrototype = { '_arrayPrototype': Array.Prototype, '_getData': function () {return this._ElementData; }, 'size': function () {return this._getData (). 길이; }, 'isempty': function () {return this.size () === 0; }, 'contains': function (obj) {return this.indexof (obj)> -1; }, 'indexof': function (obj) {var i, data = this._getData (), length = data.length; for (i = 0; i <length; i ++) {if (obj === data [i]) {return i; }} 반환 -1; }, 'lastIndexof': 함수 (obj) {var i, data = this._getData (), length = data.length; for (i = 길이 -1; i> -1; i-) {if (obj === data [i]) {return i; }} 반환 -1; }, 'get': function (index) {return this._getData () [index]; }, 'set': function (index, element) {this._getData () [index] = 요소; }, 'add': function (index, element) {if (element) {this.set (index, element); } else {return this._getData (). push (index); }}, 'remove': function (index) {var OldValue = this._getData () [index]; this._getData () [index] = null; OldValue를 반환하십시오. }, 'clear': function () {this._getData (). length = 0; }, 'addall': 함수 (index, array) {if (array) {this._getData (). splice (index, 0, array); } else {this._arrayprototype.push.apply (this._getData (), index); }}}; ArrayList.Prototype = ArrayListPrototype;// 테스트 코드 var arr = new ArrayList ([3, 6, 5, 'xyz', 'foo', 'xyz']); console.log (arr.contains ( 'xyz')); console.log (arr.indexof ( 'xyz')); console.log (arr.lastindexof ( 'xyz')); Console.log (arr.get (2)); arr.addall ([1, 2, 3]); Console.log (ARR);
위의 코드는 그 일부를 구현했으며 일부 최적화가 있습니다.
앞으로 트리, 스택, 대기열,지도 등과 같은 데이터 구조를 구현하는 클래스를 시뮬레이션하기 위해 JavaScript를 작성할 시간이 있습니다.