2012-12-11 85 views
0

我想每次將數組中的一個元素向右移動,同時將原始元素按C#中的特定順序保留。移動數組中的單個元素

好了,所以我一直要求改寫代碼,我可以理解爲什麼那麼這裏我們去:

我可能有一些48390

//the ar elements have been commented out to show that we never know what ar contains but only the that I will always want to shift; ar[4] 
    int[] ar = new int[5]; 
    //ar[0] = 4 
    //ar[1] = 8 
    //ar[2] = 3 
    //ar[3] = 9 
    //ar[4] = 0 
    while(ar != 04839) 
    { 
     Shift code 
    } 

我可能輸入5個號碼48390,如果你發現它的數字相同但只有一位數字。我想要一個while循環來旋轉4 ar [1]來移位,直到數字形成04839

我希望這是有道理的。我發佈這個問題是因爲大多數頁面發佈有關移動的信息是基於將所有元素移到右側,而我只是真的想移動一個特定的元素。

感謝您的期待。

編輯:我應該更具體。如果你不知道每個數組元素可能是什麼?所以我不能依靠「0」作爲主播。因爲另一組數字可能包含另一個數字,例如「00238」。

+1

我對你的榜樣困惑,你可以澄清的代碼? – Justin

+0

希望這能解決問題嗎?謝謝。 – Marshal

回答

0

您是否想過改用LinkedList?鏈接列表數據結構可能比數組更適合您要做的事情。 AddFirst,AddLast,AddAfter和AddBefore方法允許您以比每次重新組織數組更有效的方式將元素插入列表中。

鏈表的缺點是你需要按順序讀取元素。因此,插入/刪除元素非常有效,但是對於隨機訪問元素來說效率不高。

LinkedLists here有一個很好的概述。

1

是什麼在你的例子是一個交換,可等來實現:

private void Swap(ref int[] array, int index1, int index2) 
{ 
    int temp = array[index1]; 
    array[index1] = array[index2]; 
    array[index2] = temp; 
} 

調用Swap(ref source, 0, 1)交流會在第一和第二個元素。然後你想要的是:

for (int i = 0; i < a.Length-1; i++) 
{ 
    Swap(ref a, i, i+1); 
} 

這「泡」第一個元素到每個迭代中的最後一個位置。

0
r=ar[0]; 

for (int i = 0; ar.lenght;i++) 
{ 
ar[i]=ar[i + 1]; 
} 

ar[ar.lenght] = r; 
1

這樣如何:

  List<int> l = new List<int>(){0,6,7,8,9}; 

      for (int i=1;i<5;i++) 
      { 
       l.Remove(0); 
       l.Insert(i, 0); 
      } 
2

該方法會給你通過一個給定的陣列中插入單個元素插入(之間)的每個位置由陣列序列:

public static IEnumerable<T[]> InsertElementBetweenAllPositions<T>(
    T[] array, T element) 
{ 
    int newLength = array.Length + 1; 
    for (int i = 0; i < newLength; i++) 
    { 
     T[] rtn = new T[newLength]; 
     rtn[i] = element; 
     Array.Copy(array, 0, rtn, 0, i); 
     Array.Copy(array, i, rtn, i + 1, array.Length - i); 
     yield return rtn; 
    } 
} 

對於您的示例,您可以將其稱爲

foreach (int[] arr in InsertElementBetweenAllPositions(new[] { 6, 7, 8, 9 }, 0)) 
{ 
    foreach (int i in arr) 
     Console.Write(i + " "); 
    Console.WriteLine(); 
} 
0

也許

int oldLast = ar[ar.Length - 1]; 
for (int i = ar.Length - 1; i >= 0; i--) 
    ar[i] = i == 0 ? oldLast : ar[i - 1]; 

Demo

+0

如果你不知道ar [4]是什麼?但我仍然想用ar [4]作爲轉移的主要元素。這意味着我將無法使用一個數字來檢查它是否位於正確的位置,因爲ar [1]也可能等於「0」。有任何想法嗎?因爲這是一個while循環的最簡單和最好的選擇。謝謝。 – Marshal

1

從你需要遊移元素的例子,例如超過有點混亂,你是否需要重新循環他們身邊開始。我已經提供了下面的例子,將循環到開始 - 如果您不需要這樣做,那麼您可以重新修改if語句。

private int[] Shift(int[] a) 
{ 
    int zeroPos = Array.IndexOf(a, 0); 

    int[] rtn = new int[a.Length]; 
    a.CopyTo(rtn, 0); 

    if (zeroPos + 1 == a.Length) 
    { 
     rtn[0] = 0; 
     for (int i = 0; i < a.Length - 1; i++) 
     { 
      rtn[i + 1] = a[i]; 
     } 
    } 
    else 
    { 
     rtn[zeroPos] = rtn[zeroPos + 1]; 
     rtn[zeroPos + 1] = 0; 
    } 

    return rtn; 
} 
0

它只是一個項目的排列,下面是排列算法的完整源代碼。

static List<string> Put(char s1, string list) 
    { 
     List<string> str =new List<string>(); 

     for (int i = 0; i < list.Length+1; i++) 
     { 
      string s = list.Substring(0, i) + s1.ToString() + list.Substring(i); 
      str.Add(s); 
     } 
     return str; 
    } 
    static List<string> Permute(string list,int x) 
    { 
     List<string> Result = new List<string>(); 
     if (list.Length == 1) 
     { 
      Result.Add(list[0].ToString()); 
      return Result; 
     } 
     else 
     { 

      char first = list[0]; 
      list = list.Substring(x+1); 
      List<string> part = Permute(list,0); 
      foreach (string str in part) 
      { 
        List<string> hasBeenPlaced = Put(first, str); 
        foreach (string str2 in hasBeenPlaced) 
        { 
         Result.Add(str2); 
        } 
      } 

     } 

     return Result; 
    } 
    static void Main(string[] args) 
    { 

     List<string> per = Permute("abc",0); 
     for (int i = 0; i < per.Count; i++) 
     { 
      Console.WriteLine(per[i]); 
     } 
     Console.ReadKey(); 
    } 

現在,如果我在foreach後添加一個break,你的問題已經解決了。 (它將寫入所有permuation只是你想要的東西,不是所有的人....) 所以它改成:

 foreach (string str in part) 
     { 
      List<string> hasBeenPlaced = Put(first, str); 
      foreach (string str2 in hasBeenPlaced) 
      { 
       Result.Add(str2); 
      } 
      break; 
     } 

希望能幫助你

0

如果你LINQ,這是簡單:-)但是你需要比陣列大一些的尺寸。

ShiftLeft(ar, 1); 

private static int[] ShiftLeft(int[] value, int countOfShift = 1) 
{ 
    var length = value.Length; 

    if (countOfShift > length) 
    { 
     throw new InvalidOperationException("countOfShift must less then value's length."); 
    } 

    var tempList = new List<int>(value); 

    tempList.RemoveRange(length - countOfShift, countOfShift); 
    tempList.InsertRange(0, value.Skip(length - countOfShift)); 

    return tempList.ToArray(); 
}