Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
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

share|improve this question
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? –  Makoto Nov 18 '13 at 7:34
    
There is a Double in your list, that's all. –  RC. Nov 18 '13 at 7:35
    
you're trying to cast a double to an array of objects, exactly as the error message says. –  kritzikratzi Nov 18 '13 at 7:35
    
why casting object (which is a Double) to an array of object? –  promanski Nov 18 '13 at 7:36
    
my requirement is while list is iterated those index positions should be set for that key value. –  SriRamaChandra.G Nov 18 '13 at 7:44

2 Answers 2

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.

share|improve this answer

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();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.