简体   繁体   English

如何将一个数组中的字符添加到 C# 中的一个空数组中?

[英]How do you add characters from one array to an empty array in C#?

In the code below, I have copied the first 5 characters of my string [] items to the char itemnames.在下面的代码中,我已将字符串 [] 项目的前 5 个字符复制到 char 项目名称中。 I want to now add the new shortened strings to my empty array.我现在想将新的缩短字符串添加到我的空数组中。 I'm getting the error that "Index was outside the bounds of the array" Any idea why?我收到“索引超出数组范围”的错误知道为什么吗?

using System;

namespace new_assignment_george
{
class Program
{
    static void separate_prices(string[] items, string[] item_names, double[] item_prices)
    {
        char[] itemnames = new char[5];
        int x = 0;
        foreach (string i in items)
        {
            items[x].CopyTo(0, itemnames, 0, 5);
            item_names[x] = itemnames.ToString();
            Console.WriteLine(item_names[x]);
            x = x + 1;
        }


    }

    static void Main(string[] args)
    {
        string[] items = { "Widget 15.50", "Thingy 50.99", "Ratchet25.00", "Clanger115.49", "Fracker75.25" };
        string[] item_names = { };
        double[] item_prices = { };

        Program myprogram = new Program();

        separate_prices(items, item_names, item_prices);
    }
}

} }

You need to specify the size for the two arrays so that the indexes are available to be populated.您需要指定两个 arrays 的大小,以便可以填充索引。 you can set that like following so that you have same size of arrays for item names and prices:你可以像下面这样设置,这样你就有相同大小的 arrays 的项目名称和价格:

 string[] item_names = new string[items.Length];
 double[] item_prices = new double[item.Length];

Currently you have declared these arrays without specifying the size so those indexes which are accessed wouldn't be available to be used.目前,您已经声明了这些 arrays 而不指定大小,因此无法使用那些被访问的索引。

Now we have item_names and item_prices arrays of same size which we have in items array.现在我们有item_namesitem_prices arrays 与我们在items数组中的大小相同。

In addition to what Ehsan said, you should not be using char array here.除了 Ehsan 所说的,你不应该在这里使用 char 数组。 This code is incorrect: item_names[x] = itemnames.ToString();此代码不正确: item_names[x] = itemnames.ToString();

Here is the working code:这是工作代码:

using System;
using System.Linq;

namespace new_assignment_george
{
class Program
{
    static void separate_prices(string[] items, string[] item_names, double[] item_prices)
    {
        int i=0;
        foreach (string item in items)
        {
            item_names[i++] = item.Substring(0,5);
        }
    }

    static void Main(string[] args)
    {
        string[] items = { "Widget 15.50", "Thingy 50.99", "Ratchet25.00", "Clanger115.49", "Fracker75.25" };
        string[] item_names = new string[items.Count()];
        double[] item_prices = new double[items.Count()];
        separate_prices(items, item_names, item_prices);
        foreach(string s in item_names)
        {
            Console.WriteLine(s);
        }
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM