簡體   English   中英

將ArrayList添加到另一個ArrayList中

[英]Adding ArrayList into Another ArrayList

我的程序中有一個ArrayList,我解析一個肥皂對象,每個項目都是一個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);
       }

    }

使用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類型時,因為List does not implements Cloneable Interface

2.不要使用test.add(new ArrayList(test1)); ,如果您想添加內容而不是整個arrayList,則其輸出將為[A,B,C,D,[E,F,G,H,I]]。

使用類似這樣的東西:

contents.add(new ArrayList(tempContents));

替代方案:

contents.add(tempContents.clone());

選擇2:

contents.addAll(tempContents);

選擇1或2可將整個ArrayList作為一項插入,選擇3 ArrayList將所有項作為單獨的項添加到列表中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM