【问题标题】:Moving elements in array c#在数组c#中移动元素
【发布时间】:2021-07-29 22:18:36
【问题描述】:

我有一个非常简单的数组,我希望能够在其中移动一些项目。c# 中是否有任何内置工具可以做到这一点?如果没有,您对如何做有任何建议。

举例

var smallArray = new string[4];
smallArray[0] = "a";
smallArray[1] = "b";
smallArray[2] = "c";
smallArray[3] = "d";

假设我想(以编程方式)交换索引 2 和 0,创建

smallArray[0] = "c";
smallArray[1] = "a";
smallArray[2] = "b";
smallArray[3] = "d";

谢谢。

【问题讨论】:

  • 明确一点:“shift”是指“swap”吗?
  • 不,对不起,我的第一个例子有点模糊,我现在把这个例子讲得更清楚了。
  • Hatchet,不...它注意到一个骗局。那是一个 SWAP,我进入了 Shifting。准备好我的帖子

标签: c# arrays


【解决方案1】:

编辑:好的,现在您已经更改了示例,没有内置任何内容 - 实际上编写起来有点痛苦......您需要考虑移动它的情况“例如,向上”以及将其“向下”移动的位置。你想要单元测试,但我认为应该这样做......

public void ShiftElement<T>(this T[] array, int oldIndex, int newIndex)
{
    // TODO: Argument validation
    if (oldIndex == newIndex)
    {
        return; // No-op
    }
    T tmp = array[oldIndex];
    if (newIndex < oldIndex) 
    {
        // Need to move part of the array "up" to make room
        Array.Copy(array, newIndex, array, newIndex + 1, oldIndex - newIndex);
    }
    else
    {
        // Need to move part of the array "down" to fill the gap
        Array.Copy(array, oldIndex + 1, array, oldIndex, newIndex - oldIndex);
    }
    array[newIndex] = tmp;
}

您可能应该考虑使用List&lt;T&gt; 而不是数组,它允许您在特定索引处插入和删除。这两个操作将比仅复制相关部分更昂贵,但它会更具可读性。

【讨论】:

  • 其实那是我的错,我不是说交换,我希望能够通过设置新索引来移动元素! (更新了我的例子)。谢谢!
【解决方案2】:

这是一个现有的问题:

C# Array Move Item (Not ArrayList/Generic List)

答案是:

void MoveWithinArray(Array array, int source, int dest)
{
  Object temp = array.GetValue(source);
  Array.Copy(array, dest, array, dest + 1, source - dest);
  array.SetValue(temp, dest);
}

【讨论】:

  • 我看了那个,但没能实现。
【解决方案3】:

这解决了我的问题

var arr = new ArrayList 
    {
        "a", "b", "c", "d", "e", "f"
    };
var first = arr[2];  //pass  the value which you want move from 
var next = arr[5]; //pass the location where you want to place
var m = 0;
var k = arr.IndexOf(first, 0, arr.Count);
m = arr.IndexOf(next, 0, arr.Count);
if (k > m)
{
    arr.Insert(m, first);
    arr.RemoveAt(k + 1);
}
else
{
    arr.Insert(k, next);
    arr.RemoveAt(m + 1);
}

返回 a、b、f、c、d、e

【讨论】:

    【解决方案4】:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace SecondLargest
    {
        class ArraySorting
        {
            public  static void Main()
            {
             int[] arr={5,0,2,1,0,44,0,9,1,0,23};
    
             int[] arr2 = new int[arr.Length];
             int arr2Index = 0;
             foreach (int item in arr)
             {
               if(item==0)
               {
                   arr2[arr2Index] = item;
                   arr2Index++;
               }
             }
             foreach (int item in arr)
             {
                if(item!=0)
                {
                    arr2[arr2Index] = item;
                    arr2Index++;
                }
             }
    
             foreach (int item in arr2)
             {
                 Console.Write(item+" ");
             }
    
                Console.Read();
    
            }
        }
    }
    

    【讨论】:

    • 欢迎来到 Stack Overflow!虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    相关资源
    最近更新 更多