我有一个字符串数组,我想在中心的某处添加一个新值,但不知道如何执行此操作.任何人都可以为我制作这种方法吗?
void AddValueToArray(String ValueToAdd, String AddAfter, ref String[] theArray) {
// Make this Value the first value
if(String.IsNullOrEmpty(AddAfter)) {
theArray[0]=ValueToAdd; // WRONG: This replaces the first Val, want to Add a new String
return;
}
for(int i=0; i<theArray.Length; i++) {
if(theArray[i]==AddAfter) {
theArray[i++]=ValueToAdd; // WRONG: Again replaces, want to Add a new String
return;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Guf*_*ffa 10
您无法向数组添加项目,它始终保持相同的大小.
要获取添加了项目的数组,您需要分配一个带有一个项目的新数组,并将原始数组中的所有项目复制到新数组.
这肯定是可行的,但效率不高.你应该使用一个List<string>已经有一个Insertmetod的代替.
| 归档时间: |
|
| 查看次数: |
6432 次 |
| 最近记录: |