题
我必须使用数组进行练习。用户必须输入3个输入(每次输入有关项目的信息),输入将插入到数组中。然后我必须显示数组。
但是,我很难在不改变其中的信息的情况下增加数组的长度;以及如何允许用户输入另一组输入?这就是我到目前为止所做的:
public string stockNum;
public string itemName;
public string price;
string[] items = new string[3];
public string [] addItem(string[] items)
{
System.Console.WriteLine("Please Sir Enter the stock number");
stockNum = Console.ReadLine();
items.SetValue(stockNum, 0);
System.Console.WriteLine("Please Sir Enter the price");
price = Console.ReadLine();
items.SetValue(price, 1);
System.Console.WriteLine("Please Sir Enter the item name");
itemName = Console.ReadLine();
items.SetValue(itemName, 2);
Array.Sort(items);
return items;
}
public void ShowItem()
{
addItem(items);
Console.WriteLine("The stock Number is " + items[0]);
Console.WriteLine("The Item name is " + items[2]);
Console.WriteLine("The price " + items[1]);
}
static void Main(string[] args)
{
DepartmentStore depart = new DepartmentStore();
string[] ar = new string[3];
// depart.addItem(ar);
depart.ShowItem();
}
所以我的问题归结为:
-
如何让用户输入多批输入? 例如,用户第一次输入有关项目的信息(套接字号,价格和名称),但我需要允许用户输入有关其他项目的更多信息吗?
-
如何根据假设我在数组中有多个项目,显示数组中每个项目的套接字num,price和name?
醇>
解决方案
在.NET(C#)中,数组不可调整大小。它不像在JavaScript或PHP中那样,数组非常灵活,可以使用任意元素进行扩展。
根据定义,默认静态数组具有固定大小,因此您可以使用索引引用其中的元素。 ( http://en.wikipedia.org/wiki/Array_data_structure#Array_resizing ) 但是你可以阅读有关动态数组的内容。在C#中,它将是System.Collections.ArrayList-Object。 ( http://en.wikipedia.org/wiki/Dynamic_array )
所以你需要的是ArrayList-Definition或普通列表或通用列表。 (System.Collections.Generic.List)
string[] items = new string[3] { "input1", "input2", "input3" };
string[] moreItems = new string[10] { "input4", "input5" };
// array to list
List<string> itemsList = items.ToList<string>();
itemsList.Add("newItem");
// or merge an other array to the list
itemsList.AddRange(moreItems);
// list to array
string[] newArray = itemsList.ToArray();
其他提示
从.NET Framework 3.5开始,对于一维数组,您可以使用Array.Resize<T>
方法:
int[] myArray = { 1, 2, 3 };
Array.Resize(ref myArray, 5);
MSDN链接是此处。
我认为你可能会错误地解决这个问题......
考虑三个项目(库存号,价格和名称)是较大对象的一部分。现在我们可以称之为item
。我们想在列表或数组中存储一些<=> s。考虑使用像这样的面向对象的方法:
class Widget
{
public int StockNum;
public int Price;
public string Name;
Widget(stockNum, price, name)
{
this.StockNum= stockNum;
this.Price = price;
this.Name = name;
}
}
现在,您可以实例化这些对象的列表:
List<Widget> items = new List<Widget>();
并向他们添加项目:
for (int i = 0; i < 5; i++)
{
//Get input from user
System.Console.WriteLine("Please Sir Enter the stock number");
stockNum= Convert.ToInt32(Console.ReadLine()); //This isn't very safe...
System.Console.WriteLine("Please Sir Enter the price");
price = Convert.ToInt32(Console.ReadLine()); //This isn't very safe...
System.Console.WriteLine("Please Sir Enter the item name");
name = Console.ReadLine();
//Then add it to a new object
Widget item = new Widget(stockNum, price, name);
//And add this item to a list
items.Add(item);
}
假设你的作业要求你使用数组而不是其他集合类,那么你将不得不采取艰难的方式。
数组是不可变的,也就是说在创建它们之后你无法更改它们。您可以更改内容,但不能更改数组的大小。如果需要增大或缩小数组,则必须使用新项目数创建新数组,然后将所需项目从旧数组复制到新数组。
例如,假设你想要一个数组并且只复制偶数元素。
int[] a = new int[] {0,1,2,3,4,5,6,7,8,9};
int[] a2 = new int[a.Length / 2];
int counter = 0;
for (int i = 0; i < a.Length; i = i + 2)
{
a2[counter++] = a[i];
}
然后,您可以随时将a2指定给
a = a2;
您可以使用System.Collections.Generic.List<T>
提供可调整大小的集合,如果需要,还可以使用.ToArray()
方法返回普通数组。
使用列表,然后转换为数组。
事实上,在这个例子中,你应该拥有StockInfo {股票编号,价格,项目名称}的类或结构,然后你应该将数据输入List。最后,您可以使用列表的ToArray方法转换为StockInfo []。
您可以使用 ArrayList 吗?它会根据需要调整大小。
我遇到了同样的问题,但无法更改类型,因为我扩展了现有的API类,所以不得不坚持使用Array。这是一个扩展现有数组的简单模板方法:
public static T[] ExtendArray<T> ( T[] array , uint index) where T:new()
{
List<T> list = new List<T>();
list.AddRange(array);
while (list.Count < index) {
list.Add(new T());
}
return list.ToArray();
}
请注意,上面的使用默认构造函数初始化元素。
您可以通过使用与您期望拥有的最大数组一样大的数组来解决无法调整数组大小的“问题”。请注意,这被视为黑客攻击,而不是结构化编程!
如果您有权访问System.Linq,则可以使用方便的扩展方法 Concat 来处理IEnumerable对象,例如数组。这将允许您扩展数组:
int[] result = new int[] { 1, 2 };
int[] extend = new int[] { 3, 4 };
result = result.Concat(extend).ToArray();
foreach (int i in result)
{
Console.WriteLine(i.ToString());
}
结果: 结果1 结果2 结果3 结果,4