【发布时间】:2015-01-22 23:49:03
【问题描述】:
有没有更优雅的方法(最好在 SDK 中),它会采用现有的 N 个 String 对象数组并附加另一个 String 以便该数组现在有 N+1 个 String 对象?请看下面我是如何做到的。
import java.util.Arrays;
public class StringArrayAppend {
/**
* @param args
*/
public static void main(String[] args) {
String[] selectionArgs={"3","2"};
//Is there a SDK method to Arrays.append(selectionArgs,"1");?
int newSize=selectionArgs.length+1;
int indexNewElem=selectionArgs.length;
String[] selectionArgs2=new String[newSize];
System.arraycopy(selectionArgs, 0, selectionArgs2, 0, selectionArgs.length);
selectionArgs2[indexNewElem]="1";
selectionArgs=selectionArgs2;
selectionArgs2=null;
System.out.println("selectionArgs="+Arrays.toString(selectionArgs));
}
}
【问题讨论】:
标签: java arrays string append native