droidev
droidev

Reputation: 7390

ArrayList not initializes with predefined size

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)

Upvotes: 3

Views: 531

Answers (4)

droidev
droidev

Reputation: 7390

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

Upvotes: -1

adamclmns
adamclmns

Reputation: 29

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)

Upvotes: -1

Andy Turner
Andy Turner

Reputation: 140444

You can copy the ArrayList directly using its constructor:

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

Then there is no need to use Collections.copy.

Upvotes: 1

Eran
Eran

Reputation: 393886

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.

Upvotes: 5

Related Questions