簡體   English   中英

選擇正確的數據類型和邏輯來存儲自定義數組(如記錄(值))

[英]choosing the right data type and logic storing custom array like records(values)

說我有一個對象,它有一個[id],[name] ..和其他一些數據信息,我稱之為meta,但實際data (它的值)有點像

x:2 , y:3 l:6, t:5, n:0 ,genr:plate; x:12 , y:32 l:26, t:45, n:1 ,genr:temp;

..... x 20

最合適的dataType是什么?

因為我不需要將數組中的每個元素存儲在自己的行/ record中,而是將其用作單個記錄。

string ,您將如何選擇最佳實踐? byte array 不是以消耗內存的方式,而是性能和優雅

由於您的數據類型成對出現

x:2 , y:3 l:6, t:5, n:0 ,genr:plate; x:12 , y:32 l:26, t:45, n:1 ,genr:temp;

並且由於值可以是數字或文本,因此在C#中,請考慮使用Dictionary<string, string>存儲一行。 因此,如果你有20+線這樣,可以考慮使用ListDictionary<string, string> ,即:

List<Dictionary<string,string>> data = new List<Dictionary<string,string>>();

要添加新數據,請創建字典,拆分項目,然后將字典添加到列表中:

string line = "x:2 , y:3 l:6, t:5, n:0 ,genr:plate; x:12 , y:32 l:26, t:45, n:1 ,genr:temp;";
Dictionary<string,string> linedata = new Dictionary<string,string>();
string[] lineitems = line.Split(new char[] { ',', ';', ':' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < lineitems.Length; i += 2)
    linedata.Add(lineitems[i], lineitems[i + 1]);
data.Add(linedata); //each line is one single record in data List

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM