-1

I am having a difficulty. I have retrieved a BasicDBList from database but now I want to iterate over the list but I cannot do it in it's BasicDBList format. So I want to ask that how can we cast BasicDBList to ArrayList of Java. Here is what I have tried.

DBCursor cursor1 = coll2.find();
DBObject main1 = cursor1.next();
ArrayList<String[]> ans = new ArrayList<String[]>();
ans.add(((String[]) main1.get("Data")));

but I am having following error

Exception in thread "main" java.lang.ClassCastException: com.mongodb.BasicDBList cannot be cast to [Ljava.lang.String;

Muhammad Rafique
  • 157
  • 2
  • 15

1 Answers1

0

You are casting the BasicDBList to a String array, not an ArrayList.

Replace

ans.add(((String[]) main1.get("Data")));

with

ans.add(((ArrayList<String>) main1.get("Data")));

See the Java docs.

VHS
  • 9,534
  • 3
  • 19
  • 43