![](/img/trans.png)
[英]Is there a way to save an int 3d array as the value of a hashtable in C#?
[英]C# best way to access 3D array of structs
各位程序員朋友們好。 所以我有一個結構,並提出了 3 個不同的 3D 數組來存儲它。 問題是,優化不是我的專長,我需要了解他們關於存儲和訪問多維結構數組的最佳方法的人的建議. 我需要知道這些方法中的任何一種在內存使用或訪問速度等方面是否比彼此更好。 除了我不知道的數組之外,還有更好的方法來存儲和訪問結構嗎? 我主要使用了https://msdn.microsoft.com/en-us/library/2s05feca.aspx等堆棧溢出帖子作為參考。
我知道對於小數據集,性能和內存使用可能可以忽略不計,但我在這里發布的原因是因為我知道最終我選擇的任何方法都將用於非常大的數據集,我想避免潛在的問題。
目前,我計划對 5 x 8 x X 數組使用其中一種方法。 我將下面的每個方法初始化為 2 x 2 x X,以便更容易查看添加項目的位置。
這三種方法如下:
類數組嵌套 - 存儲在一個類中的項目數組,該類是另一個類中的數組。
public static Tier3[] _Tier3 = new Tier3[2]
3D 完整鋸齒狀陣列 - 可在所有三個維度上展開的陣列。
public static Item[][][] _3DjaggedItems = new Item[2][][]
2D 矩形數組 - 一個二維數組,用於存儲鋸齒狀數組。
public static Item[,][] _2Drectangle_3DjaggedItems = new Item[2,2][]
如果您知道存儲和訪問此結構體的更好方法,請記住您必須能夠打印出“Hello, world!”。 通過賦值給
public ItemSystem.Item _Item;
public ItemSystem.Item[] _ItemList;
並調用 PrintTest(); 打印消息。
我會盡力回答任何問題。 預先感謝您的任何幫助!
下面是我使用所有三種方法編寫的測試代碼,用於打印“Hello, world!”。 只需將粘貼復制到所選的 C# 編譯器中即可運行。 我用過http://rextester.com/
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class ItemSystem
{
public struct Item
{
public string _1;
public string _2;
public string _3;
}
public class Tier2
{
public Item[] _Items;
}
public class Tier3
{
public Tier2[] _Tier2;
}
//The class of containers the data of which is initialized once.
public static Tier3[] _Tier3 = new Tier3[2]
{
new Tier3()
{
_Tier2 = new Tier2[2]
{
new Tier2()
{
_Items = new ItemSystem.Item[]
{
new Item() { _1 = "H", _2 = "e", _3 = "l" },
new Item() { _1 = "l", _2 = "o", _3 = "," },
new Item() { _1 = " ", _2 = "w", _3 = "o" },
new Item() { _1 = "r", _2 = "l", _3 = "d" },
new Item() { _1 = "!", _2 = "", _3 = "" }
}
},
new Tier2()
{
_Items = new ItemSystem.Item[]
{
new Item() { _1 = "H", _2 = "e", _3 = "l" }
}
},
}
},
new Tier3()
{
_Tier2 = new Tier2[2]
{
new Tier2()
{
_Items = new ItemSystem.Item[]
{
new Item() { _1 = "H", _2 = "e", _3 = "l" }
}
},
new Tier2()
{
_Items = new ItemSystem.Item[]
{
new Item() { _1 = "H", _2 = "e", _3 = "l" }
}
},
}
},
};
public static Item[][][] _3DjaggedItems = new Item[2][][]
{
new Item[2][]
{
new Item[]
{
new Item() { _1 = "H", _2 = "e", _3 = "l" },
new Item() { _1 = "l", _2 = "o", _3 = "," },
new Item() { _1 = " ", _2 = "w", _3 = "o" },
new Item() { _1 = "r", _2 = "l", _3 = "d" },
new Item() { _1 = "!", _2 = "", _3 = "" }
},
new Item[]
{
new Item() { _1 = "H", _2 = "e", _3 = "l" }
},
},
new Item[2][]
{
new Item[]
{
new Item() { _1 = "H", _2 = "e", _3 = "l" }
},
new Item[]
{
new Item() { _1 = "H", _2 = "e", _3 = "l" }
},
},
};
public static Item[,][] _2Drectangle_3DjaggedItems = new Item[2,2][]
{
{
new Item[]
{
new Item() { _1 = "H", _2 = "e", _3 = "l" },
new Item() { _1 = "l", _2 = "o", _3 = "," },
new Item() { _1 = " ", _2 = "w", _3 = "o" },
new Item() { _1 = "r", _2 = "l", _3 = "d" },
new Item() { _1 = "!", _2 = "", _3 = "" }
},
new Item[]
{
new Item() { _1 = "H", _2 = "e", _3 = "l" },
},
},
{
new Item[]
{
new Item() { _1 = "H", _2 = "e", _3 = "l" },
},
new Item[]
{
new Item() { _1 = "H", _2 = "e", _3 = "l" },
},
},
};
}
public class Program
{
public ItemSystem.Item _Item;
public ItemSystem.Item[] _ItemList;
public int _Tier3Accessor = 0;
public int _Tier2Accessor = 0;
public void TestFunc()
{
_Item = ItemSystem._Tier3[_Tier3Accessor]._Tier2[_Tier2Accessor]._Items[0];
_ItemList = ItemSystem._Tier3[_Tier3Accessor]._Tier2[_Tier2Accessor]._Items;
PrintTest();
_Item = ItemSystem._3DjaggedItems[_Tier3Accessor][_Tier2Accessor][0];
_ItemList = ItemSystem._3DjaggedItems[_Tier3Accessor][_Tier2Accessor];
PrintTest();
_Item = ItemSystem._2Drectangle_3DjaggedItems[_Tier3Accessor, _Tier2Accessor][0];
_ItemList = ItemSystem._2Drectangle_3DjaggedItems[_Tier3Accessor, _Tier2Accessor];
PrintTest();
}
public void PrintTest()
{
for(int i = 0; i < _ItemList.Length; i++)
{
Console.Write(_ItemList[i]._1);
Console.Write(_ItemList[i]._2);
Console.Write(_ItemList[i]._3);
}
Console.WriteLine("\n");
}
public static void Main(string[] args)
{
Program p = new Program();
p.TestFunc();
}
}
}
弄清楚了。 進行了更多搜索,一段時間后發現了這些帖子
簡短的回答是展平數組然后計算您要訪問的索引是最快的方法。 其次是鋸齒狀數組,因為多維數組在 C# 中的實現速度很慢。
在格式化方面,鋸齒狀數組還允許您根據它們需要如何初始化而保持更有條理。 您可以對扁平數組執行相同操作,但您需要在間距和注釋方面發揮創意,以確保所有內容都在視覺上易於閱讀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.