数据结构与算法描述第(第二章示例)

//数组的浅复制
var nums = [];
for (var i = 0;i < 100;++i) {
    nums[i] = i+1;
}
var samenums = nums;
nums[0] = 400;
console.log(samenums[0]); //400
//数组的深复制
var nums = [];
for (var i = 0;i < 100;++i) {
    nums[i] = i+1;
}
function copy(arr1,arr2) {
    for(var i = 0;i < arr1.length;++i) {
        arr2[i] = arr1[i];
    }
}
copy(nums,samenums);
nums[0] = 400;
console.log(samenums[0]); //1
console.log(samenums);

//查找元素
var colors = ["red","green","blue","green"];
var fposition = colors.indexOf("green");
console.log(fposition);
var bposition = colors.lastIndexOf("green");
console.log(bposition);

//将数组转化为字符串
var strColor = colors.join("|");
console.log(strColor);
console.log(typeof strColor);
var strColor2 = colors.toString();
console.log(strColor2);

//由已有数组创建新数组
var newColor = colors.concat(["black","white"]);
console.log(newColor);
console.log(colors);
var newColor2 = colors.splice(0,2);
console.log(newColor2); //["red", "green"]
console.log(colors); //["blue", "green"]

//为数组添加元素
var forever = ["xinqi","love","dongdong","dongdong","love","xinqi"];
forever.push("!");
console.log(forever.length);
forever.unshift("dear"); //可在数组开头添加多个元素
console.log(forever.length);

//从数组中删除元素
forever = ["xinqi","love","dongdong","dongdong","love","xinqi"];
var del1 = forever.pop();
console.log(del1);
console.log(forever.length);
var del2 = forever.shift();
console.log(del2);
console.log(forever.length);

//使用自定义方法在数组开头添加元素
var nums = [2,3,4,5];
var newNum = 1;
for(var i = nums.length;i >= 0;i–) {
    nums[i] = nums[i-1];
}
nums[0] = newNum;
console.log(nums);
//使用自定义方法在数组开头移除元素
var nums = [2,3,4,5];
for (var i = 0;i < nums.length;i++) {
    nums[i] =nums[i+1];
}
nums.length = 3;
console.log(nums);

//从数组的中间位置添加和删除元素
var nums = [1,2,3,7,8,9];
var newElements = [4,5,6];
nums.splice(3,0,newElements);
console.log(nums); //1,2,3,4,5,6,7,8,9
var nums = [1,2,3,7,8,9];
nums.splice(3,0,0,1);
console.log(nums); //1,2,3,0,1,7,8,9
var nums = [1,3,4,5,6];
nums.splice(3,3);
console.log(nums);//1,3,4

//维数组排序
var nums =[1,2,3,4,5];
nums.reverse(); //5,4,3,2,1
console.log(nums);
var nums = [1,100,2];
nums.sort();
console.log(nums); //1,100,2
//比较函数
function compare(num1,num2) {
    return num1 – num2;
}
nums.sort(compare);
console.log(nums); //1,2,100

//不生成新数组迭代器方法
function square(num) {
    console.log(num * num);
}
var nums = [1,2,3];
nums.forEach(square); //1,4,9

function isEven(num) {
    return num % 2 === 0;
}
var nums = [1,2,3];
var isAllEven = nums.every(isEven);
console.log(isAllEven); //false

var issomeEven = nums.some(isEven);
console.log(issomeEven); //true

function add(runningTotal,currentValue) {
    return runningTotal + currentValue;
}
var values = [1,2,3];
var sum = nums.reduce(add);
console.log(sum); //6

function concat(accumulatedString,item) {
    return accumulatedString + item;
}
var words = ["i","love","you"];
var result = words.reduce(concat);
console.log(result); //i love you
var words = ["i","love","you"];
var resultRight = words.reduceRight(concat);
console.log(resultRight); //you love i

//生成新数组的迭代器方法
function curve(grade) {
    return grade += 5;
}
var grades = [77,65,81,92,83];
var newGrades = grades.map(curve);
console.log(newGrades); //82,70,86,97,88

function first(word) {
    return word[0];
}
var words = ["i","love","you"];
var acronym = words.map(first);
console.log(acronym.join("")); //ily

function isEven(num) {
    return num % 2 == 0;
}
function isOdd(num) {
    return num % 2 != 0;
}

var nums = [] ;
for(var i = 0;i < 20;i++) {
    nums[i] = i+1;
}
var evens = nums.filter(isEven);
console.log("偶数有:" + evens); //1~20的偶数
var odds = nums.filter(isOdd);
console.log("奇数有:" + odds); //1~20的奇数

function passing(num) {
    return num >= 60;
}
var grades = [];
for(var i = 0;i < 20;i++) {
    grades[i] = Math.floor(Math.random()*101);
}
var passGrades = grades.filter(passing);
console.log("通过考试的成绩为:" + passGrades); //

function afterc(str) {
    if(str.indexOf(‘cie’) > -1) {
        return true;
    }
    return false;
}
var words = [‘recieve’,’deceive’,’percieve’];
var misspelled = words.filter(afterc);
console.log(misspelled); //recieve,percieve

//创建二维数组
var twod = [];
var rows = 5;
for(var i = 0;i < rows;++i) {
    twod[i] = [];
} //这样创建的数组中每一个元素都是undefined

Array.matrix = function(numrows,numcols,initital){
    var arr = [];
    for(var i = 0;i < numrows;++i) {
        var columns = [];
        for(var j = 0;j < numcols;++j) {
            columns[i] = initital;
        }
        arr[i] = columns;
    }
    return arr;
};
var names = Array.matrix(3,3,"");
names[0][0] = "Joe";
console.log(names[0][0]); //Joe
console.log(names[1][0]); //undefined

var grades = [[89,77,78],[76,82,81],[91,94,89]];
console.log(grades[2][2]); //89

//处理二维数组的元素
var grades = [[89,77],[76,82,81],[91,94,89,99]];
var total = 0;
var average = 0.0;
for(var row = 0;row < grades.length;++row) {
    for(var col = 0;col < grades[row].length;++col) {
        total += grades[row][col];
    }
    average = total / grades[row].length;
    console.log("student"+parseInt(row+1)+" average:" +average.toFixed(2));
    total = 0;
    average = 0.0;
}

//对象数组
function Point(x,y) {
    this.x = x;
    this.y = y;
}
function displayPts(arr) {
    for (var i = 0;i < arr.length;++i) {
        console.log(arr[i].x + "," + arr[i].y);
    }
}
var p1 = new Point(1,2);
var p2 = new Point(3,5);
var p3 = new Point(2,8);
var p4 = new Point(4,4);
var points = [p1,p2,p3,p4];
for(var i = 0;i < points.length;++i) {
    console.log("Point" + parseInt(i+1) + ":" + points[i].x + "," +points[i].y);
}
var p5 = new Point(12,-3);
points.push(p5);
console.log("After push :");
displayPts(points);
points.shift();
console.log("After shift:");
displayPts(points);

//对象中的数组
function WeekTemps() {
    this.dataStore = [];
    this.add = add;
    this.average = average;
}
function add(temp) {
    this.dataStore.push(temp);
}
function average() {
    var total = 0;
    for(var i = 0;i < this.dataStore.length;++i) {
        total += this.dataStore[i];
    }
    return total / this.dataStore.length;
}
var thisWeek = new WeekTemps();
thisWeek.add(52);
thisWeek.add(55);
thisWeek.add(61);
thisWeek.add(65);
thisWeek.add(55);
thisWeek.add(50);
thisWeek.add(52);
thisWeek.add(49);
console.log(thisWeek.average());

IT文库 » 数据结构与算法描述第(第二章示例)
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址