[SOLVED] Java List list to String []


How would I convert

List list= new ArrayList();


String [] profArr= {};

I have tried doing

profArr = list.toArrary() 


profArr = (String [])list.toArrary()

I get the following error:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

I also have tried

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

and I get this error: The requested resource () is not available.

Here is how I create the list:

static List decode(int x)
        List power2List = new ArrayList();
        if (x < 0) 
            throw new IllegalArgumentException("Decode does not like negatives");
        while (x > 0)
            int p2 = Integer.highestOneBit(x);
            x = x - p2;
        return power2List;   

List list= new ArrayList();
list= decode(rset.getInt("favprofs")); //rset being a result set which pulls one int


You need to be using list.toArray(new String[list.size()]). An Object[] is not type compatible with String[], despite every element in the Object[] being a String. Also, you should consider specifying the type parameter of your List to maintain type safety.

Answered By – Jeffrey

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *