string [] 배열에 문자열을 추가하는 방법? .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<string>
나중에를 사용하여 배열로 바꿀 수 있습니다 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);
}
string[] coleccion = Directory.GetFiles(inputPath)
.Select(x => new FileInfo(x).Name)
.ToArray();
foreach를 사용하는 대신 for 루프를 사용하지 않는 이유는 무엇입니까? 이 시나리오에서는 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;
}
이 코드는 Android에서 스피너에 대한 동적 값 배열을 준비하는 데 효과적입니다.
List<String> yearStringList = new ArrayList<>();
yearStringList.add("2017");
yearStringList.add("2018");
yearStringList.add("2019");
String[] yearStringArray = (String[]) yearStringList.toArray(new String[yearStringList.size()]);
이 경우 배열을 사용하지 않습니다. 대신 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;
}
배열을 지우고 동시에 요소 수를 0으로 만들려면 이것을 사용하십시오.
System.Array.Resize(ref arrayName, 0);
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.
It is possible, because the type string[]
implements IEnumerable
, it also implements the following interfaces: IEnumerable<char>
, IEnumerable
, IComparable
, IComparable<String>
, IConvertible
, IEquatable<String>
, ICloneable
using System.Linq;
public string[] descriptionSet new string[] {"yay"};
descriptionSet = descriptionSet.Append("hooray!").ToArray();
'Programing' 카테고리의 다른 글
C # 배열에서 중복을 어떻게 제거합니까? (0) | 2020.05.08 |
---|---|
SSH 세션을 활성 상태로 유지 (0) | 2020.05.08 |
이메일 주소가 iOS에서 유효한지 확인하십시오 (0) | 2020.05.08 |
파일이 있는지 확인한 후 파일을 삭제하는 방법 (0) | 2020.05.08 |
뷰에서 현재 컨트롤러의 이름을 얻을 수 있습니까? (0) | 2020.05.08 |