【发布时间】:2017-03-06 12:02:12
【问题描述】:
我的应用程序从服务器获取图像 url 字符串并将它们保存在一个数组中。问题是图像 url 的数量不是恒定的,所以我想创建一个空的字符串数组,其大小根据输入的输入确定。
我用了这个Strings images=new Strings[5],我想要类似Strings images=new Strings[]的东西,所以我怎么能做到这一点,这是我的代码的一部分
String[] images4=new String[3];
String[] images5=new String[3];
String[] images7=new String[3];
String[] images6=new String[3];
String[] images1=new String[3];
String[] images2=new String[3];
String[] images3=new String[3];
String[] totals=new String[3];
try {
JSONObject json = new JSONObject(Content);
JSONArray jre = json.getJSONArray("projects");
JSONArray jreimages = json.getJSONArray("screenshots");
mArrayList = new ArrayList<Projects>();
for (int j = 0; j < jre.length(); j++) {
mProduct = new Projects();
JSONObject jobject = jre.getJSONObject(j);
String name11 = jobject.getString("name");
String description = jobject.getString("description");
String status = jobject.getString("status");
String version = jobject.getString("version");
String id =jobject.getString("id");
mProduct.setyourText(name11);
mProduct.setyourstatu(status);
mProduct.setYourversion(version);
mProduct.setyourdescription(description);
int k=0;
int l=0;
int o=0;
int p=0;
int w=0;
for (int i = 0; i < jreimages.length(); i++) {
JSONObject jjobject = jreimages.getJSONObject(i);
String imageid=jjobject.getString("project_id");
if (imageid.equals(id)) {
String urlimage = jjobject.getString("screenshot");
String total = url + urlimage;
// mess.setText(total);
if (imageid.equals("105")){
images1[k] = total;
k++;
totals=images1;
}
else if (imageid.equals("106")){
images2[k] = total;
k++;
totals=images2;
}
else if (imageid.equals("107")){
images3[k] = total;
k++;
totals=images3;
}
else if (imageid.equals("108")){
images4[k] = total;
k++;
totals=images4;
}
else if (imageid.equals("109")){
images5[k] = total;
k++;
totals=images5;
}
else if (imageid.equals("110")){
images5[k] = total;
k++;
totals=images5;
}
else if (imageid.equals("111")){
images6[k] = total;
k++;
totals=images6;
}
else{
totals=images7;
}
}
}
mProduct.setYourimages(totals);
mArrayList.add(mProduct);
mProduct = null;
}
【问题讨论】:
-
你不能为未知的最终尺寸使用正确的类型,比如
List(ArrayList),因为? -
知道大小后创建数组。声明为
String[] images5;。然后在知道大小后创建它images5 = new String[size]
标签: java android arrays string