如何更改数组中元素的位置?

时间:2017-02-25 16:31:39

标签: arrays vb.net

如果我有一个数组,例如这个

mydata <- read.table(header=TRUE, text="
Active hor plot mng
7.20    F   1   CH
8.80    O   1   CH
9.30    F   1   CH
9.20    O   1   CH
9.70    F   1   CH
9.30    O   1   CH
9.10    F   2   CH
7.50    O   2   CH
7.50    F   2   CH
8.70    O   2   CH
9.90    F   2   CH
7.60    O   2   CH
9.70    F   3   CH
7.70    O   3   CH
8.90    F   3   CH
8.60    O   3   CH
8.30    F   3   CH
8.30    O   3   CH
8.50    L   1   CH
7.40    L   1   CH
8.00    L   1   CH
9.70    L   2   CH
8.90    L   2   CH
8.40    L   2   CH
9.80    L   3   CH
8.00    L   3   CH
7.00    L   3   CH
7.30    F   1   Fe
6.60    O   1   Fe
6.50    F   1   Fe
6.60    O   1   Fe
6.90    F   1   Fe
5.80    O   1   Fe
6.60    F   2   Fe
7.00    O   2   Fe
6.00    F   2   Fe
5.10    O   2   Fe
6.10    F   2   Fe
5.10    O   2   Fe
5.10    F   3   Fe
6.50    O   3   Fe
7.70    F   3   Fe
6.90    O   3   Fe
5.20    F   3   Fe
6.30    O   3   Fe
6.50    L   1   Fe
5.00    L   1   Fe
7.80    L   1   Fe
5.10    L   2   Fe
5.50    L   2   Fe
5.60    L   2   Fe
5.50    L   3   Fe
7.80    L   3   Fe
7.70    L   3   Fe
7.20    F   1   W
8.80    O   1   W
7.80    F   1   W
7.80    O   1   W
7.90    F   1   W
8.10    O   1   W
8.60    F   2   W
7.40    O   2   W
7.40    F   2   W
8.40    O   2   W
7.70    F   2   W
8.90    O   2   W
6.70    F   3   W
6.10    O   3   W
7.50    F   3   W
8.60    O   3   W
7.80    F   3   W
8.60    O   3   W
8.30    L   1   W
8.20    L   1   W
8.70    L   1   W
8.60    L   2   W
6.80    L   2   W
6.30    L   2   W
7.30    L   3   W
7.10    L   3   W
7.70    L   3   W
")

如何按值更改每个元素的位置,例如,如果按1,Dim Players() As String = {"Adam", "Helen", "Jack", "Emily"} 将移至2,Adam将移至3,Helen应该来回到1。

如果增加1,我能够做到这一点。我将最后一个元素存储在变量中,当完成将所有元素移动1时,我会将第一个元素设置为变量(在本例中为Emily)。 / p>

如果有不同的增量值,如2,3,4或甚至6?

,我该怎么做?

编辑:任何编程语言都可以,但必须使用for循环而不是函数。

更新:我是一名IGCSE学生,这个问题已经超过一周了。

3 个答案:

答案 0 :(得分:0)

您指的是圆阵列旋转。

在VB.NET中,以下代码将按Shift量将元素数组旋转到右侧。

Sub Main()

    Dim Players() As String = {"Adam", "Helen", "Jack", "Emily"}

    Dim Players_Shifted(Players.Length - 1) As String

    Dim Shift As Integer = 2
    If Shift > Players.Length Then
        Shift = Shift Mod Players.Length
    End If

    For index = 0 To Shift - 1
        Players_Shifted(index) = Players(Players.Length - Shift + index)
    Next

    Dim index_2 = 0
    For index = Shift To Players.Length - 1
        Players_Shifted(index) = Players(index_2)
        index_2 = index_2 + 1
    Next
    Players = Players_Shifted


    ' Print out Players Array '

    For index = 0 To Players.Length - 1
        Console.Write(Players(index) + ", ")
    Next
    Console.ReadLine()
End Sub

答案 1 :(得分:0)

如果你可以增加1,只需重复所需的次数。

更好的方法是:(让我们调用增量 n );将结束 n 元素存储在另一个数组中;在此之前将元素移动到数组的末尾;将元素从另一个数组复制回到开头。像这样:

Module Module1

    Sub Main()
        Dim players() As String = {"Adam", "Becky", "Clive", "Debby", "Edward", "Fiona"}
        Dim rotateBy = 2

        ' make sure shiftBy is in a usable range
        rotateBy = rotateBy Mod players.Length
        ' if shiftBy is negative, make it positive such that a left-rotate is performed
        If rotateBy < 0 Then
            rotateBy = rotateBy + players.Length
        End If

        ' store the elements which will be moved to the other end of the array
        Dim tmp(rotateBy - 1) As String
        Dim startIndex = players.Length - rotateBy
        For i = startIndex To players.Length - 1
            tmp(i - startIndex) = players(i)
        Next

        ' move the elements
        For i = players.Length - 1 - rotateBy To 0 Step -1
            players(i + rotateBy) = players(i)
        Next

        'fill in the other elements
        For i = 0 To rotateBy - 1
            players(i) = tmp(i)
        Next

        ' show the result
        Console.WriteLine(String.Join(", ", players))

        Console.ReadLine()

    End Sub

End Module

请注意,players数组中元素的复制是向后完成的,因此重叠范围不会覆盖尚未移动的值。

要向后移动元素,请使用rotateBy的负值。

如果您使用的语言具有提供数组复制功能的功能,则此方法很容易适应使用该功能。对于任何想知道未来的人:

Module Module1

    Sub Main()
        Dim players() As String = {"Adam", "Becky", "Clive", "Debby", "Edward", "Fiona"}
        Dim rotateBy = 4

        ' make sure shiftBy is in a usable range
        rotateBy = rotateBy Mod players.Length
        ' if shiftBy is negative, make it positive such that a left-rotate is performed
        If rotateBy < 0 Then
            rotateBy = rotateBy + players.Length
        End If

        ' store the elements which will be moved to the other end of the array
        Dim tmp(rotateBy - 1) As String
        Dim startIndex = players.Length - rotateBy

        Array.Copy(players, startIndex, tmp, 0, rotateBy)
        Array.Copy(players, 0, players, rotateBy, startIndex)
        Array.Copy(tmp, players, tmp.Length)

        ' show the result
        Console.WriteLine(String.Join(", ", players))

        Console.ReadLine()

    End Sub

End Module

正如你提到的任何编程语言,这就是它在C#中作为一个函数,但不使用任何框架方法,如Array.Copy

public static void RotateUsingLoops<T>(T[] elements, int rotateBy)
{
    rotateBy = rotateBy % elements.Length;
    if (rotateBy < 0)
    {
        rotateBy += elements.Length;
    }
    T[] tmp = new T[rotateBy];
    int startIndex = elements.Length - rotateBy;

    // store the elements which will be moved to the other end of the array
    for (int i = startIndex; i < elements.Length; i++)
    {
        tmp[i - startIndex] = elements[i];
    }

    // move the elements
    for (int i = elements.Length - 1 - rotateBy; i >= 0; i--)
    {
        elements[i + rotateBy] = elements[i];
    }

    // fill in the other elements
    for (int i = 0; i < rotateBy; i++)
    {
        elements[i] = tmp[i];
    }
}

你可以使用

static void Main(string[] args)
{
    var a = new string[] { "A", "B", "C", "D" };
    RotateUsingLoops(a, 2);
    Console.WriteLine(string.Join(", ", a));

    Console.ReadLine();

}

获得输出:

  

C,D,A,B

答案 2 :(得分:-1)

以下是实现此功能的C ++代码片段 -

设temp为临时变量,N为数组的大小,shift为您想要移位的位数, arr 为原始数组。

int temp = arr[0];
int i = 0;
while(True){
    //swap(arr(i + shift)%N, temp);
    swap_var = temp;
    temp = arr[(i+shift)%N];
    arr[(i+shift)%N] = swap_var;

    i = (i+shift)%N;
    if (i == 0){
        break;
    }
}

编辑:使用for循环。

int temp = arr[0];       //change int to the type of variable ur array is
int i =0;
for (int count = 0; count<N ; count++){
    //swap(arr(i + shift)%N, temp);
    swap_var = temp;
    temp = arr[(i+shift)%N];
    arr[(i+shift)%N] = swap_var;

    i = (i+shift)%N;
}
相关问题