문자열[] 배열에 문자열을 추가하는 방법은 무엇인가요? .Add 함수는 없습니다.

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.ToArray()를 사용하여 배열로 변환할 수 있는 `List

해설 (2)

이 어레이에는 크기조정할 수도 있습니다.

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

System.Collections.Generic의 List 사용

List myCollection = new List();

…

myCollection.Add(aString);

또는 속기(컬렉션 이니셜라이저 사용):

List myCollection = new List {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;
}
해설 (2)

이지

// Create list
var myList = new List();

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

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

잘못된 # 39 m not I& 경우, 그것은:

MyArray.SetValue(ArrayElement, PositionInArray)
해설 (0)

This is how I 추가합니까 문자열으로 필요할 때.

string[] myList;
myList = new string[100];
for (int i = 0; i < 100; i++)
{
    myList[i] = string.Format("List string : {0}", i);
}
해설 (0)
string[] coleccion = Directory.GetFiles(inputPath)
    .Select(x => new FileInfo(x).Name)
    .ToArray();
해설 (0)

왜 don& # 39, t 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;
}
해설 (1)

이 코드는 준비하는 멋지구리해요 작동됨 동적 배열을 회전자 의 값을 안드로이드.

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

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

Linq ',' 및 사용 방법을 사용하여 추가에는 참조입니다 시스템드링크 제공된 확장명은 '덮어쓰기/추가': '공용 정적 IEnumerable&lt TSource>; Append&lt TSource&gt. (이 IEnumerable&lt TSource>;; 소스, 추르스 요소점) ' 그런 다음 다시 변환할지 필요한 문자열 [] '' () '' 네스토라리 사용하는 방법입니다.

있기 때문에 문자열 [] '는' 유형 '가능하다' 는 리누머이블, 또 다음 인터페이스: ',', ',', '리누머이블 IEnumerable&lt char&gt 이콩파레이블', ',', ',', '이컨베르티블 IComparable&lt String&gt String&gt', ',' 치로네이블 IEquatable&lt.

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

39 를 선택해제합니다 어레이입니다 저회가 it& 수를, s = 0 요소를 동시에 사용.

System.Array.Resize(ref arrayName, 0);
해설 (0)
string[] MyArray = new string[] { "A", "B" };
MyArray = new List(MyArray) { "C" }.ToArray();
//MyArray = ["A", "B", "C"]
해설 (1)

이 경우 꼭 이래야겠어요 어레이에서는 사용하지 않습니다. 대신 꼭 이래야겠어요 스트라이코 알렉시옹 사용합니다.

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;
}
해설 (0)