0

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.

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

    }
2
  • Please have a look at my answer Important Notes part, for getting more clearity.It will help you Commented Feb 3, 2014 at 13:49
  • Ok I will and thanks for your effort and help. Commented Feb 3, 2014 at 13:51

2 Answers 2

3

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

contents.addAll(tempContents);

What addAll(Collection c) does ?

From 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()); Only if test1 reference is of ArrayList type and not of List type as List does not implements Cloneable Interface

2. Dont Use 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]].

Sign up to request clarification or add additional context in comments.

Comments

2

Use something like this instead:

contents.add(new ArrayList(tempContents));

Alternative:

contents.add(tempContents.clone());

Alternative 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.

6 Comments

contents.add(tempContents.clone()); this one is not working on me but new ArrayList is working.
10 min rules.But I will.
Output of contents.add and contents.addAll will be different.One adds list and other add elements of list
Exactly. And that's what I say in my last two lines. So please remove your downvote. I upvoted yours in return ;)
there is nothing like a clone() method for List reference type if reference is of type ArrayList then only clone will work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.