如何延长在C#中的数组 [英] How to extend arrays in C#

查看:292
本文介绍了如何延长在C#中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用数组做练习。用户必须输入3个输入(每次约项目的信息),并输入将阵列中被插入。然后,我必须来显示阵列。

不过,我有困难的时候增加数组的长度,不改变其中的信息;我怎么可以允许用户输入另一组输入?这是我到目前为止有:

 公共字符串stockNum;
公共字符串ITEMNAME;
公共字符串的价格;字符串[] =项新的字符串[3];公共字符串[]的addItem(字符串[]项)
{
    的System.Console.WriteLine(请输入爵士股票数量);
    stockNum =到Console.ReadLine();
    items.SetValue(stockNum,0);
    的System.Console.WriteLine(请输入爵士的价格);
    价格=到Console.ReadLine();
    items.SetValue(价格,1);
    的System.Console.WriteLine(请输入爵士的项目名称);
    ITEMNAME =到Console.ReadLine();
    items.SetValue(ITEMNAME,2);
    的Array.Sort(项目);
    返回的物品;
}
公共无效ShowItem()
{
    的addItem(项目);
    Console.WriteLine(股票号码为+项目[0]);
    Console.WriteLine(的项目名称是+项目[2]);
    Console.WriteLine(价格+项目[1]);
}静态无效的主要(字串[] args)
{
    离开家百货公司新=家百货公司();
    字符串[] = AR新的字符串[3];
    // depart.addItem(AR);
    depart.ShowItem();
}

所以我的问题归结为:


  1. 我怎样才能让用户输入多个批量输入呢?
    例如,当用户第一次进入有关的项目(插座数量,价格和名称)的信息,但我需要让用户输入关于其他项目的更多信息?


  2. 我如何可以显示基于我在数组中有多个项目的假设数组中的每个项目插座NUM,价格和名称?



解决方案

在.NET(C#),数组不调整大小。这不是在JavaScript中喜欢或PHP是一个数组是由非常灵活,可以任意扩展元素

每一个定义默认静态数组有一个固定的大小,所以你可以在使用索引引用的元素。 ( http://en.wikipedia.org/wiki/Array_data_structure#Array_resizing
但是,你可以阅读有关动态数组。在C#这将是System.Collections.ArrayList对象。 ( http://en.wikipedia.org/wiki/Dynamic_array

所以,你需要的是无论是ArrayList的清晰度还是普通列表或泛型列表。 (System.Collections.Generic.List)

 的String []项目=新的字符串[3] {输入1,输入2,输入3};
字符串[] = moreItems新的字符串[10] {输入4,输入5};//数组列表
清单<串GT; itemsList = items.ToList<串GT;();itemsList.Add(的newitem);
//或合并其他的数组列表
itemsList.AddRange(moreItems);//列表数组
字符串[] newArray = itemsList.ToArray();

I have to do an exercise using arrays. The user must enter 3 inputs (each time, information about items) and the inputs will be inserted in the array. Then I must to display the array.

However, I am having a difficult time increasing the array's length without changing the information within it; and how can I allow the user to enter another set of input? This is what I have so far:

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();
}

So my question boils down to:

  1. How can I allow the user to enter more than one batch of input? For example, the first time the user will enter the information about item ( socket number, price and name), but I need to allow the user enter more information about another item?

  2. How can I display the socket num, price and name for each item in the array based on the assumption that I have more than one item in the array?

解决方案

In .NET (C#), an array isn't resizable. It's not like in JavaScript or PHP were an array is made very flexible and can be extend with arbitrary elements.

Per definition an default static array has a fix size, so you can reference an element within by using the index. ( http://en.wikipedia.org/wiki/Array_data_structure#Array_resizing ) But there you can read about dynamic array. In C# it will be the System.Collections.ArrayList-Object. ( http://en.wikipedia.org/wiki/Dynamic_array )

So what you need is either the ArrayList-Definition or a normal list or generic list. (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();

这篇关于如何延长在C#中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆