-1
Iterator<Object> itr=list.iterator();
while(itr.hasNext()){
Object []obj=(Object[]) itr.next();------------------//line1
JSONObject jo = new JSONObject();
                jo.put("routeNo", (int)obj[6]);
                jo.put("routeName", obj[5].toString());
                jo.put("stopSequenceID", (int)obj[4]);
                jo.put("stopID", (int)obj[3]);
                jo.put("stopName",obj[2].toString());
                jo.put("lat", (double)obj[1]);
                jo.put("lon", (double)obj[0]);
}
java.lang.ClassCastException: java.lang.Double cannot be cast to [Ljava.lang.Object;---------------------at line(1)

I am converting list to Object[].

As 'list' object contains 750 records. with the fields I am putting it on the 'jo' object. Each record contains those 7 fields.

Please help me . Thanks in advance

5
  • 1
    Can't be done. An Object is not an Object[]. Perhaps if you explained what you needed to do with it, then we'd be able to help you out a bit better? Also, why you've got a collection of Objects in the first place is a bit bizarre...could you elaborate as to what that collection should contain? Commented Nov 18, 2013 at 7:34
  • There is a Double in your list, that's all. Commented Nov 18, 2013 at 7:35
  • you're trying to cast a double to an array of objects, exactly as the error message says. Commented Nov 18, 2013 at 7:35
  • why casting object (which is a Double) to an array of object? Commented Nov 18, 2013 at 7:36
  • my requirement is while list is iterated those index positions should be set for that key value. Commented Nov 18, 2013 at 7:44

2 Answers 2

1

itr.next(); Returns an Object in your case. Why are you casting it to Object []?

You probably meant to do something like that:

Iterator<Double> itr=list.iterator();
ArrayList<Double> items = new ArrayList<>();
   while(itr.hasNext()) {
       items.add(itr.next());
   }

Note that defining JSONObject jo shouldn't be inside your while loop.

0

You can convert List<Double> to Object[] using toArray

  List<Double> list=new ArrayList<>();
  list.add(10.0);
  list.add(10.0);
  list.add(10.0);

 Object[] obj=list.toArray();

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.