//创建空数组 var array = new Array(); //[] //初始化数组 var array = new Array(1,2,3); var array = Array.of(1,2,3);//ES6的方法 //[1,2,3] //创建大小为5的数组 var array = new Array();//ES6的方法 //[undefined,undefined,undefined,undefined,undefined] //给数组赋值 var array = new Array(); array[0] = 1 ; array[1] = 2 ; array[2] = 3 ; //[1,2,3]
var number = [1,2,3]; number[number.length] = 4; //[1,2,3,4] //或者 var number = [1,2,3]; number.push(4); //[1,2,3,4]
var number = [1,2,3]; number.unshift(0); //[0,1,2,3] number.unshift(-2,-1); //[-2,-1,0,1,2,3]
//在索引1后面添加2,3,4 var number = [1,5,6]; number.splice(1,0,2,3,4); //[1,2,3,4,5,6]
var number = [1,2,3]; number.shift(); //[2,3]
var numebr = [1,2,3,4,5,6]; //如果想删除元素3 number.splice(2,1); //[1,2,4,5,6] //如果想删除元素4,5 number.splice(3,2);
var number = [3,2,1]; number.reverse(); //[1,2,3]
var numebr = [2,3,4,1,3,7]; number.sort(); //[1,2,3,3,4,7]
var number = [4,5,6,7,1,2,3,8,9,10,11,12,13]; number.sort(); //[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]
var number = [4,5,6,7,1,2,3,8,9,10,11,12,13];
function compare(a,b){
if(a < b){
return -1;
}
if(a > b){
return 1;
}
return 0;
}
number.sort(compare);
//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
var friends = [{name:'李晨',age:40},{name:'范冰冰',age:35}];
function comparePerson(a,b){
if(a.age < b.age){
return -1;
}
if(a.age > b.age){
return 1;
}
return 0;
}
friends.sort(comparePerson);
//[{name:'范冰冰',age:35},{name:'李晨',age:40}]
var number = [1,3,4,3,56,6,7,4]; number.indexOf(3);//1 number.lastIndexOf(3)//3
function Stack(){
var items = [];
this.push = function(element){
items.push(element);
}
this.pop = function(){
return items.pop();
}
this.peek = function(){
return items[items.length-1];
}
this.isEmpty = function(){
return items.length === 0;
}
this.size = function(){
return items.length;
}
this.clear = function(){
items = [];
}
this.print = funciton(){
console.log(items.toString());
}
}
function Queue(){
var items = [];
this.enqueue = function(element){
items.push(element);
}
this.dequeue = function(){
return items.shift();
}
this.front = function(){
return items[0];
}
this.isEmpty = function(){
return items.length === 0;
}
this.clear = function(){
items = [];
}
this.size = funciton(){
return items.length;
}
this.print = function(){
console.log(items.toString());
}
}
function PriorityQueue(){
var items = [];
funciton QueueElement(element,priority){
this.element = element;
this.priority = priority;
}
function comparePriority(a,b){
if(a.priority > b.priority){
return 1;
}
if(a.priority < b.priority){
return -1;
}
return 0;
}
this.enqueue = funciton(element,priority){
var queueElement = new QueueElement(element,priority);
items.push(queueElement);
items.sort(comparePriority);
}
//其它方法和默认的Queue实现相同
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有