将ArrayList添加到另一个ArrayList中

时间:2014-02-03 13:19:41

标签: java android arraylist reference

我的程序中有一个ArrayList,程序的某些部分我正在解析一个soap对象,每个项目都是一个tempArraylist。项目迭代完成后我将这个ArrayList添加到另一个Arraylist我的问题是它不是添加内容它是添加tempArray的引用。我可以添加数组的值而不是它的引用。

这是我的代码。

for (int i = 0; i < count; i++) 
   {
       tempContents.clear();

       Log.i(TAG , String.valueOf(count));

       Object property = response2.getProperty(i);


       if (property instanceof SoapObject)
       {
           SoapObject category_list = (SoapObject) property;



           for(int j = 0 ; j<tags.size() ; j++)
           {
               if(category_list.getProperty(tags.get(j)).toString().contains("Resim"))
               {
                   String tempResim = "htttp://www.balikesir.bel.tr/";
                   tempResim += category_list.getProperty(tags.get(j)).toString();
                   tempContents.add(tempResim);  
               }
               else
               {
                   if(category_list.getProperty(tags.get(j)).toString().equals("anyType{}"))
                   {continue;}
                   tempContents.add(category_list.getProperty(tags.get(j)).toString());

               }
           }
           for(int k = 0 ;k < tempContents.size() ; k++ )
               Log.i("For ici 5 minare",tempContents.get(k));
           Log.i("For disi 4 minare","Asdas asdas");
           contents.add(tempContents);
       }

    }

2 个答案:

答案 0 :(得分:3)

使用 public boolean addAll(Collection<? extends E> c)

contents.addAll(tempContents);

addAll(Collection c)的作用是什么?

来自Java docs

/**
 * Appends all of the elements in the specified collection to the end of
 * this list, in the order that they are returned by the
 * specified collection's Iterator.  The behavior of this operation is
 * undefined if the specified collection is modified while the operation
 * is in progress.  (This implies that the behavior of this call is
 * undefined if the specified collection is this list, and this
 * list is nonempty.)
 *
 * @param c collection containing elements to be added to this list
 * @return <tt>true</tt> if this list changed as a result of the call
 * @throws NullPointerException if the specified collection is null
 */

示例

List test = new ArrayList();
test.add("A");
test.add("B");
test.add("C");
test.add("D");
List test1 = new ArrayList();
test1.add("E");
test1.add("F");
test1.add("G");
test1.add("H");
test1.add("I");

//test.add(new ArrayList(test1));  // Dont use this if u want to add content and not entire arrayList as its output will be [A, B, C, D, [E, F, G, H, I]]



test.addAll(test1);
System.out.println(test);

<强>输出

[A, B, C, D, E, F, G, H, I]

重要提示

1。仅当test.add(test1.clone());而不是列表类型为test1 reference is of ArrayList type

时才使用List does not implements Cloneable Interface

2。不要使用test.add(new ArrayList(test1));,如果你想添加内容而不是整个arrayList,因为它的输出将是[A,B,C,D,[E,F,G] ,H,I]]。

答案 1 :(得分:2)

使用类似的东西:

contents.add(new ArrayList(tempContents));

替代:

contents.add(tempContents.clone());

备选方案2:

contents.addAll(tempContents);

选择1或2将整个ArrayList作为一个项目插入,选择3将所有项目作为单独的项目添加到列表中。