码迷,mamicode.com
首页 > 其他好文 > 详细

Array类型

时间:2017-02-15 18:19:44      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:.sh   序列   new   ons   先进先出   .so   fifo   不同   之间   

<script>
/*
JavaSrcipt数组与其他语言中的数组的区别
同:有序列表
不同:数组的每一项可以保存任何类型的数据,数组的大小可以动态调整

创建数组的方式:
1.使用Array构造函数
*/
var color= new Array()
var color= new Array(20)//创建一个包含20个项的数组
var color= new Array(‘red‘, ‘blue‘, ‘green‘)//创建一个包含3个字符串值的数组
var color= Array(20)//省略new操作符创建一个包含20个项的数组

/*
2.使用数组字面量表示法
数组字面量由一对包含数组项的方括号表示,多个数组项之间用逗号隔开
*/
var color= [‘red‘, ‘blue‘, ‘green‘]//创建一个包含3个字符串值的数组
var names= []//创建一个空数组
var values= [2,3,]//会创建一个包含2或3项的数组,建议不要这样使用
var options= [,,,,,]//会创建一个包含5或6项的数组,建议不要这样使用


/*
读取和设置数组的值
使用方括号并提供相应值的基于0的数字索引
方扩号中的所有表示要访问的值,如果索引值小于数组中的项数,则返回对应的值,如果索引值超过了数组现有最大索引值,数组就会自动增加到最大索引值加1的长度
*/
var color= [‘red‘, ‘blue‘, ‘green‘]
console.log(color[0])//显示第一项 red
color[2]= ‘black‘//修改第三项 ["red", "blue", "black"]
console.log(color)
color[3]= ‘brown‘//新增第四项 ["red", "blue", "black", "brown"]
console.log(color)

/*
length属性
始终会返回0或更大的值
*/
var colors= [‘red‘, ‘blue‘, ‘green‘]
console.log(colors.length) //3
//数组的length不是只读的,还可以从数组的末尾移除项或者向数组中添加新项
colors.length= 2
console.log(colors)// 末尾移除1项 ["red", "blue"]
colors.length= 4
console.log(colors.length)// 4 向数组中添加新项,新增的每一项都会取得undefined值
//利用length在末尾添加新项
var colors= [‘red‘, ‘blue‘, ‘green‘]
colors[colors.length]= ‘black‘//在3的位置添加
colors[colors.length]= ‘brown‘//在4的位置添加
console.log(colors)
//数组最后一项索引始终是length-1
var colors= [‘red‘, ‘blue‘, ‘green‘]
colors[99]= ‘black‘
console.log(colors)//["red", "blue", "green", 99: "black"]
console.log(colors.length)//100

 

 

/*
栈方法
栈(LIFO):last-in-fist-out 后进先出的数据结构
数组可以表现的像栈一样

push()方法:
可以接收任意数量的参数,把它们逐个添加到末尾,并返回修改后的长度

pop()方法:
从数组末尾移除最后一项,减少数组的长度,并返回移除的项
*/
var colors= new Array()
var count= colors.push(‘red‘, ‘green‘) //推入两项
console.log(count) //2
count= colors.push(‘black‘) //推入一项
console.log(count) //3

var item= colors.pop() //取得最后一项
console.log(item) //back
console.log(colors.length) //2

/*
栈方法可以与其他数组方法连用
*/
var colors= [‘red‘, ‘blue‘]
colors.push(‘brown‘)
colors[3]= ‘black‘
console.log(colors.length) //4
var item= colors.pop()
console.log(item) //black

/*
队列方法
队列(FIFO):fist-in-fist-out 先进先出
队列在列表末端添加项,在列表前端移除项

shift()方法
从数组前端取得项的方法

结合使用shift()和push()方法,可以像使用队列一样使用数组
*/
var colors= new Array();
var count= colors.push(‘red‘, ‘green‘) //推入两项
console.log(count) //2
var item= colors.shift() //取得第一项
console.log(item) //red
console.log(colors.length) //1

/*
unshift()
与shift()的用途相反,在数组前端添加任意个项,并返回新数组的长度

结合使用unshift()和pop()方法,可以在数组前端添加项,从数组末端移除项
*/
var colors= new Array()
var count= colors.unshift(‘red‘, ‘green‘)
console.log(count) //2
var item= colors.pop()
console.log(item)//green
console.log(colors.item) //undefind unshift在实现中有偏差

/*
重排序方法
1、reverse()
*/
var values= [1, 2, 3, 4, 5]
values.reverse()
console.log(values) //5,4,3,2,1
//2.sort() 升序排列
var values= [1, 3, 5, 4, 2]
values.sort()
console.log(values) //1,2,3,4,5


</script>

Array类型

标签:.sh   序列   new   ons   先进先出   .so   fifo   不同   之间   

原文地址:http://www.cnblogs.com/tiantian9542/p/6402369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!