C# 如何将字符串添加到 string[] 数组中?没有 .Add 功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1440265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:34:28  来源:igfitidea点击:

How to add a string to a string[] array? There's no .Add function

c#arrayslong-filenames

提问by Sergio Tapia

private string[] ColeccionDeCortes(string Path)
{
    DirectoryInfo X = new DirectoryInfo(Path);
    FileInfo[] listaDeArchivos = X.GetFiles();
    string[] Coleccion;

    foreach (FileInfo FI in listaDeArchivos)
    {
        //Add the FI.Name to the Coleccion[] array, 
    }

    return Coleccion;
}

I'd like to convert the FI.Nameto a string and then add it to my array. How can I do this?

我想将 转换FI.Name为字符串,然后将其添加到我的数组中。我怎样才能做到这一点?

采纳答案by Saulius Valatka

You can't add items to an array, since it has fixed length. What you're looking for is a List<string>, which can later be turned to an array using list.ToArray(), e.g.

您不能向数组添加项目,因为它具有固定长度。您正在寻找的是 a List<string>,稍后可以使用 将其转换为数组list.ToArray(),例如

List<string> list = new List<string>();
list.Add("Hi");
String[] str = list.ToArray();

回答by Adam Wright

Use List<T> from System.Collections.Generic

使用 System.Collections.Generic 中的 List<T>

List<string> myCollection = new List<string>();

…

myCollection.Add(aString);

Or, shorthand (using collection initialiser):

或者,简写(使用集合初始化程序):

List<string> myCollection = new List<string> {aString, bString}

If you really want an array at the end, use

如果你真的想要一个数组,请使用

myCollection.ToArray();

You might be better off abstracting to an interface, such as IEnumerable, then just returning the collection.

您最好抽象为接口,例如 IEnumerable,然后只返回集合。

Edit: If you mustuse an array, you can preallocate it to the right size (i.e. the number of FileInfo you have). Then, in the foreach loop, maintain a counter for the array index you need to update next.

编辑:如果你必须使用一个数组,你可以将它预先分配到正确的大小(即你拥有的 FileInfo 的数量)。然后,在 foreach 循环中,为接下来需要更新的数组索引维护一个计数器。

private string[] ColeccionDeCortes(string Path)
{
    DirectoryInfo X = new DirectoryInfo(Path);
    FileInfo[] listaDeArchivos = X.GetFiles();
    string[] Coleccion = new string[listaDeArchivos.Length];
    int i = 0;

    foreach (FileInfo FI in listaDeArchivos)
    {
        Coleccion[i++] = FI.Name;
        //Add the FI.Name to the Coleccion[] array, 
    }

    return Coleccion;
}

回答by xcud

string[] coleccion = Directory.GetFiles(inputPath)
    .Select(x => new FileInfo(x).Name)
    .ToArray();

回答by xcud

Alternatively, you can resize the array.

或者,您可以调整数组的大小。

Array.Resize(ref array, array.Length + 1);
array[array.Length - 1] = "new string";

回答by xcud

I would not use an array in this case. Instead I would use a StringCollection.

在这种情况下我不会使用数组。相反,我会使用 StringCollection。

using System.Collections.Specialized;

private StringCollection ColeccionDeCortes(string Path)   
{

    DirectoryInfo X = new DirectoryInfo(Path);

    FileInfo[] listaDeArchivos = X.GetFiles();
    StringCollection Coleccion = new StringCollection();

    foreach (FileInfo FI in listaDeArchivos)
    {
        Coleccion.Add( FI.Name );
    }
    return Coleccion;
}

回答by Stefan Nicolov

If I'm not mistaken it is:

如果我没记错的话是:

MyArray.SetValue(ArrayElement, PositionInArray)

回答by NoloMokgosi

Eazy

轻松

// Create list
var myList = new List<string>();

// Add items to the list
myList.Add("item1");
myList.Add("item2");

// Convert to array
var myArray = myList.ToArray();

回答by Sarath Rachuri

Why don't you use a for loop instead of using foreach. In this scenario, there is no way you can get the index of the current iteration of the foreach loop.

为什么不使用 for 循环而不是使用 foreach。在这种情况下,您无法获得 foreach 循环当前迭代的索引。

The name of the file can be added to the string[] in this way,

文件名可以通过这种方式添加到string[]中,

private string[] ColeccionDeCortes(string Path)
{
  DirectoryInfo X = new DirectoryInfo(Path);
  FileInfo[] listaDeArchivos = X.GetFiles();
  string[] Coleccion=new string[listaDeArchivos.Length];

  for (int i = 0; i < listaDeArchivos.Length; i++)
  {
     Coleccion[i] = listaDeArchivos[i].Name;
  }

  return Coleccion;
}

回答by kittugadu

This is how I add to a string when needed:

这是我在需要时添加到字符串的方式:

string[] myList;
myList = new string[100];
for (int i = 0; i < 100; i++)
{
    myList[i] = string.Format("List string : {0}", i);
}

回答by Taras Vovkovych

This code works great for preparing the dynamic values Array for spinner in Android:

此代码非常适合为 Android 中的微调器准备动态值数组:

    List<String> yearStringList = new ArrayList<>();
    yearStringList.add("2017");
    yearStringList.add("2018");
    yearStringList.add("2019");


    String[] yearStringArray = (String[]) yearStringList.toArray(new String[yearStringList.size()]);