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`을 문자열로 변환한 다음 내 배열에 추가하고 싶습니다. 어떻게 하면 되나요?
System.Collections.Generic의 List
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();
왜 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;
}
이 코드는 준비하는 멋지구리해요 작동됨 동적 배열을 회전자 의 값을 안드로이드.
List<String> yearStringList = new ArrayList<>();
yearStringList.add("2017");
yearStringList.add("2018");
yearStringList.add("2019");
String[] yearStringArray = (String[]) yearStringList.toArray(new String[yearStringList.size()]);
Linq ',' 및 사용 방법을 사용하여 추가에는 참조입니다 시스템드링크 제공된 확장명은 '덮어쓰기/추가': '공용 정적 IEnumerable< TSource>; Append< TSource>. (이 IEnumerable< TSource>;; 소스, 추르스 요소점) ' 그런 다음 다시 변환할지 필요한 문자열 [] '' () '' 네스토라리 사용하는 방법입니다.
있기 때문에 문자열 [] '는' 유형 '가능하다' 는 리누머이블, 또 다음 인터페이스: ',', ',', '리누머이블 IEnumerable< char> 이콩파레이블', ',', ',', '이컨베르티블 IComparable< String> String>', ',' 치로네이블 IEquatable<.
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;
}