3

I have an ArrayList of type my model, which conatins 3 items, that is the ArrayList size is 3.

ArrayList<Model> mModels; // mModels.size() = 3

I have to copy this ArrayList in to another ArrayList, so for that I have created another ArrayList of same type as follows.

ArrayList<Model> localModels = new ArrayList<>(mModels.size());

next step is to copy data from member variable to local variable, since I dont want to copy the reference of member variable I have used Collections.copy()

Collections.copy(localModels,mModels);

but I am getting ArrayOutOfBoundException by telling destination size should be greater than source size. so I have logged both variable size. then for the member I got the size as 3 and for the localVariable logged the size as 0.

UPDATE

and I have tried copiying member ArrayList to local one. but it copies only reference. is there any way to copy the data instead of reference ?

I have tried these methods

 //1        
       for(Model model: mModels){    
             localModels.add(model);
        }

 //2    
       for(Model model: mModels){    
          localModels.add((Model)model.clone());
       }

 //3
       Collections.copy(localModels, mModels);

 //4
       localModels = (ArrayList<Model>)mModels.clone();

 //5 
       localModels = new ArrayList<>(mModels);

so my questions are

1- how can I copy (value change should not reflect) value from one ArrayList to another ?

2- why java/android always copiying the reference

3- how to intialize ArrayList with predefined size (already answered)

droidev
  • 7,352
  • 11
  • 62
  • 94
  • My bad, if you want to copy an arraylist from an arraylist , you can just do `List newList = new ArrayList(oldList);` – BrockLee Sep 28 '15 at 13:55
  • @Sotirios How this qn marked as duplicate. The question you pointed and this are entirely different. – droidev Sep 28 '15 at 16:48
  • You seem to have confused the initial capacity as the size. Those are two different things. You're trying copy three elements into a list that does't have three elements. – Sotirios Delimanolis Sep 28 '15 at 20:09
  • @SotiriosDelimanolis actually I read that question before posting this question. I posted this question only because it is not of my case. – droidev Sep 29 '15 at 04:21
  • If you think the posts in the link don't provide an answer to your question, please edit your question to explain why you think so. – Sotirios Delimanolis Sep 29 '15 at 04:22
  • @SotiriosDelimanolis you can reload the question now – droidev Sep 29 '15 at 04:33

4 Answers4

5
ArrayList<Model> localModels = new ArrayList<>(mModels.size());

creates an ArrayList whose initial capacity is equal to mModels.size() (3 in your example), but it contains 0 elements.

You have to add elements to the ArrayList in order for it to have a positive number of elements.

If you want your localModels ArrayList to contain the same elements as mModels, you can initialize it with :

ArrayList<Model> localModels = new ArrayList<>(mModels); 

This will give you a shallow copy of the original ArrayList though.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

You can copy the ArrayList directly using its constructor:

ArrayList<Model> localModels = new ArrayList<>(mModels);

Then there is no need to use Collections.copy.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
-1

Array Lists have dynamic size, so you don't need to initialize it with a pre-defined size. You can just use ArrayList.addall() to add the contents of one List object to another.

See the Docs here - http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#addAll(java.util.Collection)

adamclmns
  • 29
  • 3
-1

finally I found the answer:

I have to copy each fields in the Models separately to second ArrayList

like

ArrayList<Model> localModels = new ArrayList<>(mModels.size());

for(Model model: mModels){
 Models tempModel = new Model();
 tempModel.setId(model.getId());
 tempModel.setName(model.getName());
 ....
 ....
 localModels.add(tempModel);
}

return localModels();

this is the way to copy contents inside one ArrayList to another ArrayList. any other method posted in question will copy only reference

droidev
  • 7,352
  • 11
  • 62
  • 94