在原位反转阵列为什么不起作用? [英] Reversing an Array in Place why does it not work?

查看:91
本文介绍了在原位反转阵列为什么不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function reverseArray(array) {
    var reversed_array = [];
    for(var i = 0; i < array.length; i++) {
        reversed_array.unshift(array[i]);
    }

    return reversed_array;
}

function reverseArrayInPlace(array) {
    console.log(array); //[1,2,3,4,5] 

    array = reverseArray(array);
    console.log(arguments); //{0: [5, 4, 3, 2, 1]} this is good.
    console.log(array);    // [5, 4, 3, 2, 1] this is also good.
    return array;         //should return [5, 4, 3, 2, 1]   
}

console.log(reverseArray(["A", "B", "C"]));   //["C", "B", "A"]


var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);

console.log(arrayValue);  // [1, 2, 3, 4, 5] *wrong*

这是我想要像array.reverse()一样用reverseArrayInPlace()的返回值覆盖变量(arrayValue)的问题.

here is the problem I want to override the variable (arrayValue) with the return value of reverseArrayInPlace() just like array.reverse() does.

这是解决问题的方法

function reverseArrayInPlace(array) {

  var half = Math.floor(array.length / 2);

  for(var i = 0; i < half; i++) {
    var current = array[i];
    array[i] = array[array.length - 1 - i];
    array[array.length - 1 - i] = current;
  }

  console.log(array);    // [5, 4, 3, 2, 1] this is good.
  return array;         //should return [5, 4, 3, 2, 1]
}

var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);

console.log(arrayValue);  // [5, 4, 3, 2, 1]   *it works!*

如果两个reverseArrayInPlace()函数都具有相同值的return语句,我不明白为什么该解决方案可以用新值替换(arrayValue).

I don't understand why this solution works in replacing (arrayValue) with the new value, if both reverseArrayInPlace() functions have a return statement with the same value.

有人可以解释一下我对编程还很陌生吗,谢谢!

Can someone please explain I'm fairly new to programming, thanks!

推荐答案

第一个代码示例的问题是: 调用reverseArrayInPlace(arrayValue);时,没有将函数调用的结果分配给arrayValue,因此arrayValue不会改变.

The problem with your first code example is this: When you call reverseArrayInPlace(arrayValue); you are not assigning the result of that function call to arrayValue, so arrayValue doesn't change.

arrayValue = reverseArrayInPlace(arrayValue);

将解决该问题.

现在,为什么第一个示例中的reverseArrayInPlace函数不会更改实际的数组元素,而第二个示例却如此:

Now for why the reverseArrayInPlace function in your first example doesn't change the actual array elements, but the second example does:

当您将数组作为变量传递给函数时,它是通过引用传递的,而不是通过值传递的,因此,您实质上是将指针(array)传递给该函数,它将与该函数一起使用以访问数组的实际内容.该引用仅存在于函数范围内,因此对其进行更改不会影响实际的数组.

When you pass an array as a variable to a function it is passed by reference, not by value, so you are essentially passing a pointer (array) to the function, which it is going to work with to access the actual content of the array. This reference only exists inside the scope of the function, so changing it does not affect the actual array.

array = reverseArray(array);

这里您要完全更改此引用,但是此更改仅适用于reverseArrayInPlace函数内部,因此不会更改您的实际数组.但是,在第二个示例中,您将直接访问原始数组并更改其元素.这就是为什么您的第二个示例有效而第一个示例无效的原因.

Here you are changing exactly this reference, but this change only applies inside the reverseArrayInPlace function and hence does not change your actual array. In your second example, however you are accessing the original array directly and changing its elements. This is why your second example works, and your first one doesn't.

这篇关于在原位反转阵列为什么不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆