Compiling my code with -Xlint I found this:
warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.ArrayList<java.lang.String>
ArrayList<String> msgData = (ArrayList<String>)element;
This is the code:
ArrayList<ArrayList<String>> msg = new ArrayList<ArrayList<String>>();
//add some data to msg
Iterator i = msg.iterator();
while(i.hasNext()) {
Object element = i.next();
ArrayList<String> msgData = (ArrayList<String>)element;
}
how should the cast be done?
Your iterator should be something like
Iterator<ArrayList<String>> i = msg.iterator();
Btw you can use for-each loop for it:
for (ArrayList<String> sublist : msg) {
....
}
Your Iterator needs a type as well, like this:
ArrayList<ArrayList<String>> msg = new ArrayList<ArrayList<String>>();
//add some data to msg
Iterator<ArrayList<String>> i = msg.iterator();
while(i.hasNext()) {
ArrayList<String> element = i.next();
}
On a side note, it's better to program to interfaces, not to implementations. Thus, you'd write:
List<List<String>> msg = new ArrayList<List<String>>();
//add some data to msg
Iterator<List<String>> i = msg.iterator();
while(i.hasNext()) {
List<String> element = i.next();
}
ArrayList<ArrayList<String>> msg = new ArrayList<ArrayList<String>>();
//add some data to msg
for (ArrayList<String> msgData : msg) {
//do something
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With