user975044
user975044

Reputation: 385

Java List list to String []

How would I convert

List list= new ArrayList();

to

String [] profArr= {};

I have tried doing

profArr = list.toArrary() 

    and

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;
            power2List.add(p2);
        }
        return power2List;   
    }

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

Upvotes: 0

Views: 1463

Answers (3)

smart lph
smart lph

Reputation: 131

you can use java collector

List<Tuple2<String, Boolean>> paras_check
String miss_params= paras_check.stream().map(e->e._1)
            .reduce(",",(x,y)->(x+y));

Upvotes: 0

S.P.
S.P.

Reputation: 3054

Basically you need to use

String profArr = list.toArray( < String array > (size))

Upvotes: 0

Jeffrey
Jeffrey

Reputation: 44798

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.

Upvotes: 8

Related Questions