我想动态地举例如下String[] images
,但我无法实现它
String[] images = new String[] { "http://image/image1", "http://image/image1", "http://image/image1"};
我有一个json,其中包含图片网址,下面代码是我如何将json图片网址放到string[] images
String[] images = new String[]{};
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String imagepath = c.getString("imagepath");
images[i] = imagepath; // trying to put the values to String[] images
}
答案 0 :(得分:3)
int lenth=contacts.length();
String[] images = new String[lenth];
for (int i = 0; i < lenth; i++) {
JSONObject c = contacts.getJSONObject(i);
String imagepath = c.getString("imagepath");
images[i] = imagepath ; // trying to put the values to String[] images
}
为了更好地循环,请关注Performance Tips。
答案 1 :(得分:3)
尝试使用Arraylist而不是数组
ArrayList<String> arraylist=new ArrayList<>();
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String imagepath = c.getString("imagepath");
arraylist.add(imagepath);
}
审核项目使用arraylist.get(i);
答案 2 :(得分:2)
在使用之前,您必须为String[]
定义尺寸。
String[] images = new String[contacts.length()];
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String imagepath = c.getString("imagepath");
images[i] = newURL; // trying to put the values to String[] images
}