我有以下代码:
String[] where;
where.append(ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1");
where.append(ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1");
这两个追加没有编译。这是如何正确工作的?
我有以下代码:
String[] where;
where.append(ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1");
where.append(ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1");
这两个追加没有编译。这是如何正确工作的?
当前回答
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);
}
其他回答
向字符串数组添加新项。
String[] myArray = new String[] {"x", "y"};
// Convert array to list
List<String> listFromArray = Arrays.asList(myArray);
// Create new list, because, List to Array always returns a fixed-size list backed by the specified array.
List<String> tempList = new ArrayList<String>(listFromArray);
tempList.add("z");
//Convert list back to array
String[] tempArray = new String[tempList.size()];
myArray = tempList.toArray(tempArray);
也可以预先分配足够大的内存大小。下面是一个简单的堆栈实现:程序应该输出3和5。
class Stk {
static public final int STKSIZ = 256;
public int[] info = new int[STKSIZ];
public int sp = 0; // stack pointer
public void push(int value) {
info[sp++] = value;
}
}
class App {
public static void main(String[] args) {
Stk stk = new Stk();
stk.push(3);
stk.push(5);
System.out.println(stk.info[0]);
System.out.println(stk.info[1]);
}
}
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);
}
您可以创建一个数组列表,并使用Collection.addAll()将字符串数组转换为您的数组列表
它不是编译,因为数组没有名为append的函数更好,正确的方法是使用ArrayList
import java.util.ArrayList;
ArrayList where = new ArrayList<String>();
where.add(ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1")
where.add(ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1")