array類:
首先 ^^
陣列定義習慣上都用 ,
下面分別使用 new array() 與
,驗證時間效率**如下,看看誰用的時間少:
var a = new date ;以下總結參考 jsvar arr, direct, b = ;
for(var i=0; i<10000; i++)
arr = new array();
b.push(new date() - a);
a = new date;
for(var i=0; i<10000; i++)
direct = ;
b.push(new date() - a);
alert(b.join(" : "));
高階程式設計
1 建立 array 類物件:
(1)var numbers= new array();//習慣上是var numbers= ;
numbers [0] = "one";
numbers [2] = "two";
numbers [3] = "three";
numbers [35] = "four";
alert(numbers [0]); //outputs 「one」
alert(numbers [35]); //outputs 」four」
alert(numbers.length); //outputs 「36」
(2)var numbers= new array(15);// 習慣上是var numbers= [15];
alert(numbers [0]); //outputs "undefined"
alert(numbers.length); //outputs "15"
(3)var numbers= new array("one","two","three");//習慣上是var numbers= ["one","two","three"];
alert(numbers [0]); //outputs "undefined"
numbers [10] = "four";
alert(numbers.length); //outputs "11"
(4)用字面量表示定義array物件:2 array 物件覆蓋 tostring() 方法和 valueof() 方法,返回字串。var numbers= ["one","two","three"];
alert(numbers [0]); //outputs "undefined"
numbers [10] = "four";
alert(numbers.length); //outputs "11"
var numbers= ["one","two","three"];alert(numbers.tostring()); //outputs "one,two,three"
alert(numbers.valueof()); //outputs "one,two,three"
3 tolocalstring()
方法。
var numbers= ["one","two","three"];alert(numbers.tolocalestring()); //outputs "one,two,three"
4 join()
方法,,用來連線字串的值。
var numbers= ["one","two","three"];5 concat()alert(numbers.join(",")); //outputs "one,two,three"
alert(numbers.join("-and-")); //outputs "one-and-two-and-three"
alert(numbers.join("")); //outputs "onetwothree"
alert(numbers.join(")(")); //outputs "one)(two)(three"
方法。
var numbers= ["one","two","three"];var arr = numbers;
var numbers2=arr.concat("four","five");
alert(numbers2.tostring()); //outputs "one,two,three,four,five"
alert(numbers.tostring()); //outputs "one,two,three"
//alert(numbers.concat("four","five")); //outputs "one,two,three"
6 slice()
方法。
var numbers = ["one","two","three","four","five","six"];var arr = numbers;
var numbers2 = arr.slice(1);
alert(numbers2.tostring()); //outputs "two,three,four,five,six"
var numbers3 = arr.slice(1,4);
alert(numbers3.tostring()); //outputs "two,three,four"
7 呼叫 push()
和 pop()
方法實現後進先出的結構(棧 stack
)。
var stack= ["one","two","three"];stack.push("four");
stack.push("five","six");
alert(stack.tostring()); //outputs "two,three,four,five,six"
var removelast=stack.pop();
alert(removelast); //outputs "six"
var removelast=stack.pop();
alert(removelast); //outputs "five"
alert(stack.tostring()); //outputs "two,three,four"
8 呼叫 shift()
和 push()
方法實現後進後出結構(佇列 queue
)。
var queue= ["one","two","three"];9 其它。queue.push("four");
alert(queue.tostring()); //outputs "one,two,three,four"
var removefirst = queue.shift();
alert(removefirst); //outputs "one"
alert(queue.tostring()); //outputs "two,three,four"
(1)unshift()
方法把乙個項新增在陣列的第乙個位置。
(**的上面部分可以用 8
的**)
var addfirst = queue.unshift("one");//alert(addfirst); //outputs "4"
alert(queue.tostring()); //outputs "one,two,three,four"
(2)reverse()
方法顛倒陣列項順序。
**銜接上面的 unshift()
。
queue.reverse();alert(queue.tostring()); //outputs "four,three,two,one"
( 3 ) sort() 方法按陣列項的公升序排列。
var queue= ["one","two","three"];( 4 ) splice() 方法。第一引數是指起始位置,第二個引數是要刪除項的個數,第三個引數是要插入的項。queue.sort();
alert(queue.tostring()); //outputs "one,three,two"
var queue= ["one","two","three","four","five"];queue.splice(0,2);
alert(queue.tostring()); //outputs "three,four,five",delete one and two
queue.splice(1,1,"and1","and2","and3");
alert(queue.tostring()); //outputs "three,and1,and2,and3,four"
js array 物件方法總結
1.push 後增 push 向陣列的末尾新增乙個或多個元素,並返回新的長度 尾部新增 var a 1,2,3 var b a.push 4 console.log a 1,2,3,4 console.log b 4 2.unshift 前增 unshift 向陣列的開頭新增乙個或多個元素,並返回新...
js Array物件的常用方法總結
一 檢測陣列 array.isarray 確定某個值是不是陣列 var arr 1,2,3 array.isarray arr true二 轉換方法 1.tostring 返回由陣列中每個值的字串形式拼接而成的乙個以逗號分隔的字串 2.valueof 返回陣列本身 var colors red bl...
區間第k小類問題總結
剛碰到區間第k小的問題的時候只會區間sort,後來才掌握了一些解決此類問題的方法。區間第k小有的題目帶修改有的不帶修改,不論是什麼,都有對應的解決方法。1.我們可以分塊。分成根號n塊,塊內排序。對於每次查詢l,r我們二分答案 二分乙個值,統計區間內比他小的個數 對於不完整的區間,我們掃一遍統計,對於...