2013-04-19 176 views
1

我正在尋找一種將4個一維數組輸入到4x4多維數組的方法。將一維數組插入到多維數組中

在我花費在尋找這個的時候,我發現使用鋸齒狀數組看起來簡單得多。但是,我覺得我錯過了一些明顯的東西,並希望尋求幫助。

for (int x = 0; x <= 3; x++) 
{ 
    //reads in 4 separate values e.g. A B C D 
    unitReader = sr.ReadLine(); 

    //creates a char array with 4 separate elements 
    char[] line = unitReader.ToCharArray(); 

    //places that array into a bigger jagged array 
    fullArray[x] = line; 

    //just to test that it's worked 
    Console.WriteLine(fullArray[x]); 
} 

這是怎麼了,我已經能夠用其定義交錯數組做到這一點早爲:

char[][] fullArray = new char[4][]; 

是否有類似下面的代碼,讓我通過線賦值到一個多維數組,而無需做16次遍歷來分配單個元素?

+0

爲什麼你不喜歡16個指定? – shibormot

+0

@shibormot - 可伸縮性的擔憂也許?也許他的陣列有可能是16 x 16或32 x 32? – Tim

+0

@bootski檢查這篇文章:http://blogs.msdn.com/b/ericlippert/archive/2008/09/22/arrays-considered-somewhat-harmful.aspx數組有時只需要性能問題 – shibormot

回答

0

我認爲你正在尋找Buffer.BlockCopy()

有關詳細信息,請參閱Documentation of BufferCopy.BlockCopy

+0

啊,很好。我會研究這一點,我會回來,並標記爲答案,如果是的話。看起來有希望 – bootski

+0

這是我能找到的唯一方法,它允許我以簡單(ish)的方式提問。然而,由於這個解決方案和擴展的複雜性,我決定堅持鋸齒陣列。感謝minhcat_vo。 – bootski

0

可擴展性,你可以嘗試這樣的

public class FixedSizeCollection<T> : Collection<T> 
{ 
    protected bool _initializing; 
    public int Size { get; private set; } 

    public FixedSizeCollection(int size) 
    { 
     Size = size; 
     Init(); 
    } 

    public FixedSizeCollection(int size, IList<T> list) 
    { 
     Size = size; 
     Init(); 
     if (list.Count != Size) 
      throw new InvalidOperationException("Changing size is not supported."); 

     foreach (T item in list) 
      Items[list.IndexOf(item)] = item; 
    } 

    protected virtual void Init() 
    { 
     _initializing = true; 
     base.ClearItems(); 
     for (int j = 0; j < Size; j++) 
      Add(default(T)); 
     _initializing = false; 
    } 

    protected override void ClearItems() 
    { 
     Init(); 
    } 

    protected override void InsertItem(int index, T item) 
    { 
     if (!_initializing) 
      throw new InvalidOperationException("Changing size is not supported."); 
     base.InsertItem(index, item); 
    } 

    protected override void RemoveItem(int index) 
    { 
     if (!_initializing) 
      throw new InvalidOperationException("Changing size is not supported."); 
     base.RemoveItem(index); 
    } 

    protected override void SetItem(int index, T item) 
    { 
     base.SetItem(index, item); 
    } 
} 

public class SquareArray<T> : FixedSizeCollection<FixedSizeCollection<T>> 
{ 
    public SquareArray(int size) : base(size) 
    { 
    } 

    protected override void Init() 
    { 
     _initializing = true; 
     for (int i = 0; i< Size; i++) 
     { 
      FixedSizeCollection<T> row = new FixedSizeCollection<T>(Size); 
      Add(row); 
     } 
     _initializing = false; 
    } 

    protected override void SetItem(int index, FixedSizeCollection<T> item) 
    { 
     if (item.Count != Size) 
      throw new InvalidOperationException("Changing size is not supported."); 
     base.SetItem(index, item); 
    } 
} 

那麼你的自定義集合循環將看起來像這樣:

SquareArray<char> fullarray = new SquareArray(4); 

for (int x = 0; x <= 3; x++) 
{ 
    //reads in 4 separate values e.g. A B C D 
    unitReader = sr.ReadLine(); 

    //creates a char array with 4 separate elements 
    char[] line = unitReader.ToCharArray(); 

    //places that array into a bigger jagged array 
    fullArray[x] = new FixedSizeCollection(4, line); 

    //just to test that it's worked 
    Console.WriteLine(fullArray[x]); 
} 
+0

謝謝。雖然這可能確實解決了可伸縮性問題,但它沒有解決多維數組問題。另外,這對我來說有點過於複雜,因爲我對此很陌生(正如你可能知道的那樣)。 – bootski