数组 2017-03-19

Posted 茶瓶儿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数组 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;

 

以上是关于数组 2017-03-19的主要内容,如果未能解决你的问题,请参考以下文章

2017/03/19学习笔记

django开发实战笔记-1-2017-03-19

2017-03-19 codeforces 664C 找规律,水

08.18 javascript 06 数组 数组的概念 创建数组 读取数组中的元素 稀疏数组 添加和删除数组的元素 数组遍历 多维数组 数组的方法 类数组对象 作为数组的字符串

C 语言数组 ( 数组指针 | 数组指针定义 | 使用 数组类型* 定义数组指针 )

C 语言数组 ( 数组指针 | 数组指针定义 | 使用 数组指针类型 定义数组指针 )