make arrayList.toArray() return more specific types

JavaArraysObjectTypesArraylist

Java Problem Overview


So, normally ArrayList.toArray() would return a type of Object[]....but supposed it's an Arraylist of object Custom, how do I make toArray() to return a type of Custom[] rather than Object[]?

Java Solutions


Solution 1 - Java

Like this:

List<String> list = new ArrayList<String>();

String[] a = list.toArray(new String[0]);

Before Java6 it was recommended to write:

String[] a = list.toArray(new String[list.size()]);

because the internal implementation would realloc a properly sized array anyway so you were better doing it upfront. Since Java6 the empty array is preferred, see https://stackoverflow.com/questions/174093

If your list is not properly typed you need to do a cast before calling toArray. Like this:

    List l = new ArrayList<String>();

    String[] a = ((List<String>)l).toArray(new String[l.size()]);

Solution 2 - Java

It doesn't really need to return Object[], for example:-

	List<Custom> list = new ArrayList<Custom>();
	list.add(new Custom(1));
	list.add(new Custom(2));
	
	Custom[] customs = new Custom[list.size()];
	list.toArray(customs);
	
	for (Custom custom : customs) {
		System.out.println(custom);
	}

Here's my Custom class:-

public class Custom {
	private int	i;

	public Custom(int i) {
		this.i = i;
	}

	@Override
	public String toString() {
		return String.valueOf(i);
	}
}

Solution 3 - Java

A shorter version of converting List to Array of specific type (for example Long):

Long[] myArray = myList.toArray(Long[]::new);

(available from java 11)

Solution 4 - Java

Solution 5 - Java

 public static <E> E[] arrayListToTypedArray(List<E> list) {

    if (list == null) {
      return null;
    }
    int noItems = list.size();
    if (noItems == 0) {
      return null;
    }

    E[] listAsTypedArray;
    E typeHelper = list.get(0);

    try {
      Object o = Array.newInstance(typeHelper.getClass(), noItems);
      listAsTypedArray = (E[]) o;
      for (int i = 0; i < noItems; i++) {
        Array.set(listAsTypedArray, i, list.get(i));
      }
    } catch (Exception e) {
      return null;
    }

    return listAsTypedArray;
  }

Solution 6 - Java

I got the answer...this seems to be working perfectly fine

public int[] test ( int[]b )
{
    ArrayList<Integer> l = new ArrayList<Integer>();
    Object[] returnArrayObject = l.toArray();
    int returnArray[] = new int[returnArrayObject.length];
    for (int i = 0; i < returnArrayObject.length; i++){
         returnArray[i] = (Integer)  returnArrayObject[i];
    }

    return returnArray;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkamikaze_pilotView Question on Stackoverflow
Solution 1 - JavaMihai ToaderView Answer on Stackoverflow
Solution 2 - JavalimcView Answer on Stackoverflow
Solution 3 - JavagiltslView Answer on Stackoverflow
Solution 4 - JavacorsiKaView Answer on Stackoverflow
Solution 5 - JavaAli BagheriView Answer on Stackoverflow
Solution 6 - JavaAshvithaView Answer on Stackoverflow