的NumberOfItemsInArray
看起来很没用,因为你有Array.Lenght
,我认为你的阵列是不是阵列,我认为这是字典,如果是的话那么看看这个:
using System;
using System.Collections.Generic;
namespace Program {
class _Main {
// Initialize dictionary with values
static Dictionary<string, string> dictionaryWithStrings = new Dictionary<string, string> {
{ "NumberOfItemsInArray", "2" },
{ "Item1-Name", "Some name" },
{ "Item1-Price", "0.5$" },
{ "Item1-DiscountRate", "?%" },
{ "Item1-Category", "Some category" },
{ "Item2-Name", "Other name" },
{ "Item2-Price", "4$" },
{ "Item2-DiscountRate", "24%" },
{ "Item2-Category", "Other category" }
};
// Create *dynamic* list with Item's
static List<Item> objects = new List<Item>();
static void Main() {
// "- 1" removes the NumberOfItemsInArray from count
// "/ 4" Since you have always 4 strings
for (int i = 1; i <= (dictionaryWithStrings.Count - 1)/4; i++) {
objects.Add(new Item {
Name = dictionaryWithStrings["Item"+i+"-Name"],
Price = dictionaryWithStrings["Item"+i+"-Price"],
DiscountRate = dictionaryWithStrings["Item"+i+"-DiscountRate"],
Category = dictionaryWithStrings["Item"+i+"-Category"]
});
}
Console.WriteLine("objects contains [" + objects.Count + "] items, values:");
foreach (Item item in objects) {
Console.WriteLine("Item id: [" + objects.IndexOf(item) +
"], Name: [" + item.Name +
"], Price: [" + item.Price +
"], DiscountRate: [" + item.DiscountRate +
"], Category: [" +item.Category + "]"
);
}
}
struct Item {
public string Name;
public string Price;
public string DiscountRate;
public string Category;
}
}
}
请**阐明您的具体问题**或添加其他详细信息以确切地突出显示您需要的内容。正如目前所写,很难确切地说出你在问什么。请参阅[如何提问](http://stackoverflow.com/help/how-to-ask)页面以获得澄清此问题的帮助。 –
基本上我想循环遍历一个看起来像所描述的数组,并将每个组的4个字符串分配给一个新的独立数组。你如何用for循环做到这一点? –
你尝试了什么? –