java 数组 移动位置_将数组元素从一个数组位置移动到另一数组位置

我很难弄清楚如何移动数组元素。 例如,给出以下内容:

var arr = [ 'a', 'b', 'c', 'd', 'e'];

我为什么能写入移动功能'd'之前, 'b' ?

或'a'后'c' ?

移动之后,应更新其余元素的索引。 这意味着在第一个示例中,移动后arr [0] ='a',arr [1] ='d'arr [2] ='b',arr [3] ='c',arr [4] = 'e'

这看起来应该很简单,但是我无法将其包裹住。

#1楼

您可以实现一些基本的微积分,并创建通用函数以将数组元素从一个位置移动到另一位置。

对于JavaScript,它看起来像这样:

function magicFunction (targetArray, indexFrom, indexTo) {

targetElement = targetArray[indexFrom];

magicIncrement = (indexTo - indexFrom) / Math.abs (indexTo - indexFrom);

for (Element = indexFrom; Element != indexTo; Element += magicIncrement){

targetArray[Element] = targetArray[Element + magicIncrement];

}

targetArray[indexTo] = targetElement;

}

请查看“积木”中的“移动数组元素”以获取详细说明。

#2楼

Array.move.js

摘要

在数组中移动元素,返回包含已移动元素的数组。

句法

array.move(index, howMany, toIndex);

参量

index :元素移动的索引。 如果为负, 索引将从末尾开始。

howMany :要从索引移动的元素数。

toIndex :放置移动元素的数组的索引。 如果为负,则toIndex将从结尾开始。

用法

array = ["a", "b", "c", "d", "e", "f", "g"];

array.move(3, 2, 1); // returns ["d","e"]

array; // returns ["a", "d", "e", "b", "c", "f", "g"]

Polyfill

Array.prototype.move || Object.defineProperty(Array.prototype, "move", {

value: function (index, howMany, toIndex) {

var

array = this,

index = parseInt(index) || 0,

index = index < 0 ? array.length + index : index,

toIndex = parseInt(toIndex) || 0,

toIndex = toIndex < 0 ? array.length + toIndex : toIndex,

toIndex = toIndex <= index ? toIndex : toIndex <= index + howMany ? index : toIndex - howMany,

moved;

array.splice.apply(array, [toIndex, 0].concat(moved = array.splice(index, howMany)));

return moved;

}

});

#3楼

最后,我将这两种方法结合起来,无论在长距离还是短距离移动时,效果都更好。 我得到了相当一致的结果,但是这可能会被比我聪明的人进行一些调整,以针对不同的尺寸进行不同的工作,等等。

当移动物体较小距离时,使用其他一些方法比使用拼接要快得多(x10)。 但是,这可能会根据数组的长度而变化,但是对于大型数组而言确实如此。

function ArrayMove(array, from, to) {

if ( Math.abs(from - to) > 60) {

array.splice(to, 0, array.splice(from, 1)[0]);

} else {

// works better when we are not moving things very far

var target = array[from];

var inc = (to - from) / Math.abs(to - from);

var current = from;

for (; current != to; current += inc) {

array[current] = array[current + inc];

}

array[to] = target;

}

}

#4楼

splice()方法在数组中添加/删除项目,并返回删除的项目。

注意:此方法更改原始数组。 / w3schools /

Array.prototype.move = function(from,to){

this.splice(to,0,this.splice(from,1)[0]);

return this;

};

var arr = [ 'a', 'b', 'c', 'd', 'e'];

arr.move(3,1);//["a", "d", "b", "c", "e"]

var arr = [ 'a', 'b', 'c', 'd', 'e'];

arr.move(0,2);//["b", "c", "a", "d", "e"]

由于该函数是可链接的,因此也可以工作:

alert(arr.move(0,2).join(','));

#5楼

我的2c。 易于阅读,有效,快速,不会创建新数组。

function move(array, from, to) {

if( to === from ) return array;

var target = array[from];

var increment = to < from ? -1 : 1;

for(var k = from; k != to; k += increment){

array[k] = array[k + increment];

}

array[to] = target;

return array;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值