简体   繁体   English

将ArrayList添加到另一个ArrayList中

[英]Adding ArrayList into Another ArrayList

I have an ArrayList in my program and some part of the program I'm parsin an soap object and every item is one tempArraylist.After the item iteration is finished I'm adding this ArrayList into another Arraylist my problem is it's not adding the content it is adding the reference of the tempArray.How can I add the value of array not reference of it. 我的程序中有一个ArrayList,我解析一个肥皂对象,每个项目都是一个tempArraylist。在项目迭代完成后,我将此ArrayList添加到另一个Arraylist中,我的问题是它没有添加内容它添加了tempArray的引用。如何添加数组的值而不是它的引用。

Here is my code. 这是我的代码。

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);
       }

    }

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

contents.addAll(tempContents);

What addAll(Collection c) does ? addAll(Collection c)有什么作用?

From Java docs , 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
 */

EXAMPLE

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);

Output 产量

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

Important Notes 重要笔记

1. Use test.add(test1.clone()); 1.使用test.add(test1.clone()); Only if test1 reference is of ArrayList type and not of List type as List does not implements Cloneable Interface 仅当test1 reference is of ArrayList type而不是List类型时,因为List does not implements Cloneable Interface

2. Dont Use test.add(new ArrayList(test1)); 2.不要使用test.add(new ArrayList(test1)); ,if u want to add content and not entire arrayList as its output will be [A, B, C, D, [E, F, G, H, I]]. ,如果您想添加内容而不是整个arrayList,则其输出将为[A,B,C,D,[E,F,G,H,I]]。

Use something like this instead: 使用类似这样的东西:

contents.add(new ArrayList(tempContents));

Alternative: 替代方案:

contents.add(tempContents.clone());

Alternative 2: 选择2:

contents.addAll(tempContents);

Choose 1 or 2 for inserting the whole ArrayList as one item and 3 for adding all the items as separate items to the list. 选择1或2可将整个ArrayList作为一项插入,选择3 ArrayList将所有项作为单独的项添加到列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM