簡體   English   中英

"<i>How to add a string to a string[] array?<\/i>如何將字符串添加到 string[] 數組?<\/b> <i>There&#39;s no .Add function<\/i>沒有 .Add 功能<\/b>"

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

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

我想將FI.Name轉換為字符串,然后將其添加到我的數組中。 我怎樣才能做到這一點?

您不能向數組添加項目,因為它具有固定長度。 您正在尋找的是List<string> ,稍后可以使用list.ToArray()將其轉換為數組,例如

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

或者,您可以調整數組的大小。

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

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

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

…

myCollection.Add(aString);

或者,簡寫(使用集合初始化程序):

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

如果你真的想要一個數組,請使用

myCollection.ToArray();

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

編輯:如果你必須使用一個數組,你可以將它預先分配到正確的大小(即你擁有的 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;
}

輕松

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

如果我沒記錯的話是:

MyArray.SetValue(ArrayElement, PositionInArray)

這是我在需要時添加到字符串的方式:

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

為什么不使用 for 循環而不是使用 foreach。 在這種情況下,您無法獲得 foreach 循環當前迭代的索引。

文件名可以通過這種方式添加到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;
}
string[] coleccion = Directory.GetFiles(inputPath)
    .Select(x => new FileInfo(x).Name)
    .ToArray();

此代碼非常適合為 Android 中的微調器准備動態值數組:

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


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

要清除數組並同時使其元素數= 0,請使用此..

System.Array.Resize(ref arrayName, 0);

在這種情況下我不會使用數組。 相反,我會使用 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;
}
string[] MyArray = new string[] { "A", "B" };
MyArray = new List<string>(MyArray) { "C" }.ToArray();
//MyArray = ["A", "B", "C"]

using System.Linq;添加對 Linq 的引用using System.Linq; 並使用提供的擴展方法Appendpublic static IEnumerable<TSource> Append<TSource>(this IEnumerable<TSource> source, TSource element)然后您需要使用.ToArray()方法將其轉換回string[]

這是可能的,因為類型string[]實現了IEnumerable ,它還實現了以下接口: IEnumerable<char> , IEnumerable , IComparable , IComparable<String> , IConvertible , IEquatable<String> , ICloneable

using System.Linq;
public string[] descriptionSet new string[] {"yay"};
descriptionSet = descriptionSet.Append("hooray!").ToArray();  

創建擴展:

public static class TextFunctions
{
    public static string [] Add (this string[] myArray, string StringToAdd)
    {
          var list = myArray.ToList();
          list.Add(StringToAdd);
          return list.ToArray();
    }
}

我會這樣做:

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

foreach (FileInfo FI in listaDeArchivos)
{
    Coleccion = Coleccion.Concat(new string[] { FI.Name }).ToArray();
}

return Coleccion;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM