# Java – ArrayList is not initialized with a predefined size

ArrayList is not initialized with a predefined size… here is a solution to the problem.

## ArrayList is not initialized with a predefined size

I have an ArrayList of my model type with 3 items, i.e. the ArrayList size is 3.

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

I

had to copy this ArrayList to another ArrayList, so I created another ArrayList of the same type as shown below.

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

The next step is to copy the data from the member variable to the local variable, because I don’t want to copy the reference to the member variable I use with Collections.copy().

``````Collections.copy(localModels,mModels);
``````

But I get ArrayOutOfBoundException by telling the target size should be greater than the source size. So I recorded two variable sizes. Then for members, I get a size of 3, and for localVariable, I record the size as 0.

Update

And I’m trying to copy the member ArrayList to the local member. But it only copies the reference. Is there any way to copy data instead of references?

I’ve tried all of these methods

`````` //1
for(Model model: mModels){
}

2
for(Model model: mModels){
}

3
Collections.copy(localModels, mModels);

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

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

So my question is

1- How do I copy values from one ArrayList (value changes should not reflect) to another?

2- Why does java/android always copy references

3- How to initialize the ArrayList with a predefined size (answered).

### Solution

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

Create an `ArrayList` with an initial capacity equal to `mModels.size() (`3 in your example), but it contains 0 elements.

You must add elements to `the ArrayList` so that it has a positive number of elements.

If you want your `localModels` `ArrayList` to contain the same elements as `mModels`, you can initialize it using the following method:

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

However, this will give you a shallow copy of the original `ArrayList`.
{"ft_lang_label":"__label__en","ft_lang_prob":0.79961723,"math_prob":0.82530797,"size":2006,"snap":"2023-40-2023-50","text_gpt3_token_len":454,"char_repetition_ratio":0.19430569,"word_repetition_ratio":0.006644518,"special_character_ratio":0.22482552,"punctuation_ratio":0.13350785,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95935744,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-27T04:24:44Z\",\"WARC-Record-ID\":\"<urn:uuid:9587551c-6ef6-4451-8c70-6022929051ec>\",\"Content-Length\":\"50304\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bbc620cc-9b13-42cf-83a6-a10cf6dcdd86>\",\"WARC-Concurrent-To\":\"<urn:uuid:5a713a88-d7cb-4a72-a4e2-d612b2bad28c>\",\"WARC-IP-Address\":\"172.67.167.80\",\"WARC-Target-URI\":\"https://etbye.com/java-arraylist-is-not-initialized-with-a-predefined-size/\",\"WARC-Payload-Digest\":\"sha1:XILE47N4HL4U6YTAPCXWHXZUU5DACNRH\",\"WARC-Block-Digest\":\"sha1:SEIRWRU6XHEXH464MIBFAJJXATUL7245\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510259.52_warc_CC-MAIN-20230927035329-20230927065329-00308.warc.gz\"}"}