0

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
3
  • What is your original list of? Does it have a toString() Method implemented? Commented Mar 6, 2012 at 2:07
  • The message about "The requested resource () is not available." has nothing to do with this code; something weird just happened that one time. Commented Mar 6, 2012 at 2:08
  • Also have you tried list.toArray(new String[0]) Commented Mar 6, 2012 at 2:09

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I get: The requested resource () is not available. mmm i wonder if something else is wrong then
@user975044 Without seeing more of your code or the full error message, I can only assume something else is wrong.
0

Basically you need to use

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

2 Comments

This code won't even compile. list.toArray returns an array, not a String.
I meant this one which seems to have already been provided as an answer list.toArray( new String[list.size]) where in my case <String array is basically any string object array and its size definition>
0

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.