【问题标题】:creat empty array of strings创建空的字符串数组
【发布时间】: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


【解决方案1】:

您可以使用两种方法:

  1. String[] arr;
  2. ArrayList&lt;String&gt; arrList= new ArrayList&lt;&gt;();

【讨论】:

    【解决方案2】:

    不要创建单独的字符串数组。您可以使用 Arraylist 制作动态字符串数组。

    ArrayList<String> images =new ArrayList<String>();
    

    插入值。只需使用 add() 方法。 例如,你想存储 iamgeUrls :

    images.add(image1);
    images.add(image2);
    

    并使用 get() 方法检索数据。示例:

    images.get(position); //where position is the index of imageUrl you want to retrieve.
    

    【讨论】:

      【解决方案3】:

      你可以使用:

      String[] array= new String[0];
      

      或者

      ArrayList<String> array=new ArrayList<String>();
      

      【讨论】:

        猜你喜欢
        • 2013-11-29
        • 2020-02-18
        • 1970-01-01
        • 2017-07-30
        • 1970-01-01
        • 1970-01-01
        • 2016-03-10
        • 1970-01-01
        相关资源
        最近更新 更多