数组 2017-03-19

一、三种定义方式:

(1)       var arr=Array(x);  ----大写

arr[k]=”a”;

注:x为数组长度,弱类型语言中一般不写; k为索引值,0开始计数;a为value。

(2)       var attr=[“a”,”b”];

(3)       var attr=Array(“a”,”b”); 或者var attr= new Array(“a”,”b”);  一般不加new.

 

Var x=attr.length;  表示数组长度。

arr.push(“d”); 将加入数组arr中。

常用第一种和第二种;注意书写方式

二、遍历数组

 第一种:

for(var i=0;i<attr3.length;i++){

                                                 alert(attr3[i]);

                                          }

第二种:

for(var k in arr){

                                                 alert(k + "-------" +arr[k]);

                                          }

   注: k为名字,不是索引值,但等同于索引值,0开始

        arr(k)是数组的值。

三、例题

1、求一个数组中的最大值、最小值和总和。

<script>

            var attr=Array();

            for(var i=0; i<5; i++){

                   var arr= prompt("请输入第"+(i+1)+"个数据");

                   attr.push(arr);

            }

            for(var x in attr){

                   alert(attr[x]);

            }

            var sum=0;

            var max=attr[0];

            var min=attr[0];

            for(var x in attr){

                   sum+=parseInt(attr[x]);

                   if (attr[x]>max){

                          max=attr[x];

                   }

                   if (attr[x]<min){

                          min=attr[x];

                   }

            }

            alert(min+"+"+max+"+"+sum);

            </script>

2、输入去重

var attr = Array();

                                        for(var i = 0; i < 5; i++) {

                                               var x = prompt("请输入第" + (i + 1) + "个数据")

                                               var status = false;

                                              

                                               for(var y in attr){

                                                      if(attr[y] == x)

                                                      {

                                                             status = true;

                                                      }

                                               }

                                               if(status == true)

                                               {

                                                      continue;

                                               }

                                               attr.push(x);

                                        }

                                       

                                        for(var z in attr)

                                        {

                                               alert(attr[z]);

                                        }

难点:设置初始值。

3、冒泡排序法(重点)

     var attr = Array();

                   for(var i = 0; i < 5; i++) {

                          attr[i] = parseInt(prompt("请输入第" + (i + 1) + "个数字"));

                   }

 

                   for(var x = 0; x < 5; x++) {

                          for(var y = x + 1; y < 5; y++) {

                                 if(attr[x] < attr[y]) {

                                        var temp = attr[x];

                                        attr[x] = attr[y];

                                        attr[y] = temp;

                                 }

                          }

                   }

                   for(var k in attr)

                   {

                          alert(attr[k]);

                   }

难点:数据交换:

var a = 1;

                                        var b = 2;

                                        var temp;

                                        temp = a;

                                        a = b;

                                        b = temp;

 

posted @ 2017-03-19 09:20  陈观爱  阅读(159)  评论(3编辑  收藏  举报