C# 如何将字符串添加到字符串[]数组中?那里';s no.Add函数

C# 如何将字符串添加到字符串[]数组中?那里';s no.Add函数,c#,arrays,long-filenames,C#,Arrays,Long Filenames,我想将FI.Name转换为字符串,然后将其添加到我的数组中。如何执行此操作?使用System.Collections.Generic中的列表 private string[] ColeccionDeCortes(string Path) { DirectoryInfo X = new DirectoryInfo(Path); FileInfo[] listaDeArchivos = X.GetFiles(); string[] Coleccion; foreac

我想将
FI.Name
转换为字符串,然后将其添加到我的数组中。如何执行此操作?

使用System.Collections.Generic中的列表

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;
}
您最好抽象到一个接口,比如IEnumerable,然后返回集合

Array.Resize(ref array, array.Length + 1);
array[array.Length - 1] = "new string";
编辑:如果必须使用数组,则可以将其预分配到正确的大小(即您拥有的文件信息数)。然后,在foreach循环中,为下一步需要更新的数组索引维护一个计数器

myCollection.ToArray();

无法向数组中添加项,因为它具有固定长度。您要查找的是一个
列表
,稍后可以使用
List.ToArray()
,将其转换为数组,例如

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;
}
List List=新列表();
列表。添加(“Hi”);
字符串[]str=list.ToArray();

或者,您可以调整阵列的大小

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

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

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

如果我没有弄错的话,它是:

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;
}
易怒的

//创建列表
var myList=新列表();
//将项目添加到列表中
myList.添加(“第1项”);
myList.添加(“第2项”);
//转换为数组
var myArray=myList.ToArray();

为什么不使用for循环而不是foreach呢。在这个场景中,您无法获得foreach循环当前迭代的索引

文件名可以这样添加到字符串[]

// 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();
private string[]ColeccionDeCortes(字符串路径)
{
DirectoryInfo X=新的DirectoryInfo(路径);
FileInfo[]listaDeArchivos=X.GetFiles();
string[]Coleccion=新字符串[listaDeArchivos.Length];
for(int i=0;i
这是我在需要时添加到字符串的方式:

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[]myList;
myList=新字符串[100];
对于(int i=0;i<100;i++)
{
myList[i]=string.Format(“列表字符串:{0}”,i);
}

这段代码非常适合在Android中为spinner准备动态值数组:

string[] myList;
myList = new string[100];
for (int i = 0; i < 100; i++)
{
    myList[i] = string.Format("List string : {0}", i);
}
List yearStringList=newArrayList();
年度清单。添加(“2017”);
年度清单。添加(“2018”);
年度清单。添加(“2019”);
字符串[]yearStringArray=(字符串[])yearStringList.toArray(新字符串[yearStringList.size()]);

要清除数组并同时使其元素数=0,请使用此项

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


    String[] yearStringArray = (String[]) yearStringList.toArray(new String[yearStringList.size()]);
string[]MyArray=新字符串[]{“A”,“B”};
MyArray=新列表(MyArray){“C”}.ToArray();
//MyArray=[“A”、“B”、“C”]

使用System.Linq添加对Linq的引用
并使用提供的扩展方法
Append
公共静态IEnumerable Append(此IEnumerable源、TSource元素)
然后需要使用
.ToArray()
方法将其转换回
字符串[]

这是可能的,因为类型
字符串[]
实现了
IEnumerable
,它还实现了以下接口:
IEnumerable
IEnumerable
IComparable
IComparable
IConvertible
IComparable

string[] MyArray = new string[] { "A", "B" };
MyArray = new List<string>(MyArray) { "C" }.ToArray();
//MyArray = ["A", "B", "C"]

我会这样做:

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

我的join方法只接收字符串[]数组,不能使用列表。我有没有办法用数组来解决这个问题?使用一个列表,然后当你需要数组时,调用ToArray方法+1。我会继续,并提供一个链接到我关于这个问题的答案。“您不能将项目添加到数组”-实际上您可以,请参见下文。
array.Resize
是调整数组大小的正确方法。如果在代码段之前添加一条注释,指出这很少是处理数组表示可调整大小的集合的情况的最佳方法,则会得到+1.:)实际上,这是(唯一)能准确解决OP问题的答案。*
Add
,而不是
Add
。解释为什么数组在这里是一个不好的选择可能是个好主意,OP应该从列表开始。唯一的问题是:您必须知道
listaDeArchivos
的长度。如果您不这样做(例如,如果它可能发生变化,并且由于对象是嵌套模型或模型字段,可能/可能未填充,因此提前进行计数会很复杂),并且您只想执行
string[]Coleccion然后像
intidx=0那样分配它;Coleccion[idx]=文件名;idx++,它将告诉您
未赋值局部变量'Coleccion'的使用
。仅供参考,任何想要将其调整为更具活力的人都会遇到一个陷阱。
    List<String> yearStringList = new ArrayList<>();
    yearStringList.add("2017");
    yearStringList.add("2018");
    yearStringList.add("2019");


    String[] yearStringArray = (String[]) yearStringList.toArray(new String[yearStringList.size()]);
System.Array.Resize(ref arrayName, 0);
string[] MyArray = new string[] { "A", "B" };
MyArray = new List<string>(MyArray) { "C" }.ToArray();
//MyArray = ["A", "B", "C"]
using System.Linq;
public string[] descriptionSet new string[] {"yay"};
descriptionSet = descriptionSet.Append("hooray!").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;