javaScript扫盲之数组

之前学过java,所以学习javaScript基本语法的时候,有比较着学习。javaScript语法要宽泛许多。

看到数组的时候,觉得不同之处还蛮多的,就写一些例子,日后看起来也方便。

 

 

var names = Array("zhao","qian","sun","li");
	for(var i=0;i<names.length;i++)
	{
		document.write(names[i] + " ");
	} 
	//zhao qian sun li 
	

	names = ["zhou","wu","zheng","wang"];
	for(var i=0;i<names.length;i++)
	{
		document.write(names[i] + " ");
	}
	//zhou wu zheng wang 
		

	names = Array(3);
	names[0] = "zhao";
	names[1] = "qian";
	for(var i=0;i<names.length;i++)
	{
		document.write(names[i] + " ");
	}
	//zhao qian undefined


	//即使初始化数组长度为3,可实际赋给其四个值,数组长度也变为4.
	names = Array(3);
	document.write("names.length = " + names.length);
	//names.length = 3
	names[0] = "zhao";
	names[1] = "qian";
	names[2] = "sun";
	names[3] = "li";
	document.write("names.length = " + names.length);
	//names.length = 4
	for(var i=0;i<names.length;i++)
	{
		document.write(names[i] + " ");
	}
	//zhao qian sun li



//数据类型不限
	names = [2012,"July",30.5,true,30.0500];
	for(var i=0;i<names.length;i++)
	{
		document.write(names[i] + " ");
	}
	//2012 July 30.5 true 30.05(无效的0滤掉了)  



//关联数组
  names = Array();
  names["zhou"] = "zhou lili";  
  names["wu"] = "wu yuanyuan";
  document.write(names["zhou"] + "    " + names["wu"]);
	//zhou lili wu yuanyuan(会忽略掉空白符)
 

 

你可能感兴趣的:(javascript 数组)