2013-04-19 211 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