English 中文(简体)
如何在扼杀阵列中添加插图? 无。 增加职能
原标题:How to add a string to a string[] array? There s no .Add function
  • 时间:2009-09-17 17:38:24
  •  标签:
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";

Use List<T> from System.Collections. 通用

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

…

myCollection.Add(aString);

或者,简短(使用收集初步数据):

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

如果你真的想要一个阵列,使用

myCollection.ToArray();

你也许可以更妥善地选择一个接口,例如电子电子数据,然后回到收集工作。

Edit: 如果你must 利用阵列,你可以预先将其分配到适当的规模(即你拥有的档案信息的数量)。 然后,在座标栏中,保持一个对照面,以备日后更新所需的阵列指数。

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

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

如果我不错的话,那就是:

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,而是使用 for。 在这种情形下,你无法获得目前 for虫的指数。

档案名称可以这种方式添加到座标上,

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

这部法典在为安汶的pin子准备动态价值方面做了大量工作:

    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 = new string[] { "A", "B" };
MyArray = new List<string>(MyArray) { "C" }.ToArray();
//MyArray = ["A", "B", "C"]

Adding a reference to Linq using System.Linq; and use the provided extension method Append: public static IEnumerable<TSource> Append<TSource>(this IEnumerable<TSource> source, TSource element) Then you need to convert it back to string[] using the .ToArray() method.

由于string[] 落实 可计数的,该代码还采用了以下接口:IE amountable<char> 可计数的,,>编号>IComparable/code>,IComparable<String>, ,,>IEquatable<String>,

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

因此,如果你重新添加更多的内容,你不知道你们中有多少人能够更好地利用系统清单。 收集。 通用课程。

我在此不使用一个阵列。 相反,我将使用“强角”。

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

同时,要清除阵列,使其数量达到零。

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

设定范围:

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

并以此为基础:

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

我想这样做:

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;




相关问题