我有以下代码:

String[] where;
where.append(ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1");
where.append(ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1");

这两个追加没有编译。这是如何正确工作的?


当前回答

你可以简单地这样做:

System.arraycopy(initialArray, 0, newArray, 0, initialArray.length);

其他回答

如果你真的想要调整数组的大小,你可以这样做:

String[] arr = {"a", "b", "c"};
System.out.println(Arrays.toString(arr)); 
// Output is: [a, b, c]

arr = Arrays.copyOf(arr, 10); // new size will be 10 elements
arr[3] = "d";
arr[4] = "e";
arr[5] = "f";

System.out.println(Arrays.toString(arr));
// Output is: [a, b, c, d, e, f, null, null, null, null]

您可以创建一个数组列表,并使用Collection.addAll()将字符串数组转换为您的数组列表

数组大小不可修改。如果你必须使用数组,你可以使用:

System.arraycopy(src, srcpos, dest, destpos, length); 
String[] source = new String[] { "a", "b", "c", "d" };
String[] destination = new String[source.length + 2];
destination[0] = "/bin/sh";
destination[1] = "-c";
System.arraycopy(source, 0, destination, 2, source.length);

for (String parts : destination) {
  System.out.println(parts);
}

Apache Commons Lang有

T[] t = ArrayUtils.add( initialArray, newitem );

它返回一个新数组,但如果你真的因为某种原因使用数组,这可能是最理想的方法。