---
title: "Best way to convert a collection into an array in java"  
description: "Best way to convert a collection into an array in java"  
author: "Anonymous User"  
published: 2015-10-08  
updated: 2015-10-08  
canonical: https://www.mindstick.com/forum/33500/best-way-to-convert-a-collection-into-an-array-in-java  
category: "java"  
tags: ["java", "collection", "array"]  
reading_time: 1 minute  

---

# Best way to convert a collection into an array in java

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to convert [ArrayList](https://www.mindstick.com/articles/11973/arraylist-class-in-c-sharp) into [integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer) array, but its throwing CTE, Is it [possible to convert](https://answers.mindstick.com/qa/96585/is-it-possible-to-convert-a-presentation-into-a-video-if-yes-how) directly a [collection](https://www.mindstick.com/articles/1718/collections-in-java) into an array i.e. is there any build-in [utility](https://www.mindstick.com/articles/12979/the-utility-of-accounting-tools-for-businesses-and-organizations) [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) or [method](https://www.mindstick.com/forum/166/webservice-method) for doing this thing only?

## Replies

### Reply by Anonymous User

Hi Elizabeth, There is nothing build-in to [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) a collection to an array as far as i know.You may need to create a method which use Iterator or listIterator(in case of list) and iterate on all the elements of the collection through it and convert it by type casting into prmitive type and assign the value in the array. Pass your collection inside this method. folloe below code :

```
public static int[] convertIntegers(List<Integer> integers){    int[] ret = new int[integers.size()];    Iterator<Integer> iterator = integers.iterator();    for (int i = 0; i < ret.length; i++)    {        ret[i] = iterator.next().intValue();    }    return ret;}
```

\
\


---

Original Source: https://www.mindstick.com/forum/33500/best-way-to-convert-a-collection-into-an-array-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
