Reputation: 169
I am using the following code:
{
// ...
String[] roles = new String[resultList.size()];
int i=0;
for (Iterator<Object[]> iter = resultList.iterator(); iter.hasNext();) {
roles[i] = new String();
Object[] objArr = iter.next();
roles[i] = objArr[0].toString();
i++;
}
return roles;
}
However, I get a ClassCastException
saying cannot cast from java.lang.String to Object[].
Upvotes: 0
Views: 249
Reputation: 1395
try this:
{
// ...
String[] roles = new String[resultList.size()];
int i=0;
for (Iterator<String> iter = resultList.iterator(); iter.hasNext();) {
roles[i] = iter.next();
i++;
}
return roles;
}
Upvotes: 1
Reputation: 796
Can you make this:
Object[] objArr = iter.next();
Into this:
String[] objArr = (String[]) iter.next();
Upvotes: 0
Reputation: 130887
Try this to convert an Object
list to a String
array:
// Create an object list and add some strings to it
List<Object> objectList = new ArrayList<>();
objectList.add("A");
objectList.add("B");
objectList.add("C");
// Create an String array with the same size of the object list
String[] stringArray = new String[objectList.size()];
// Iterate over the object list to fill the string array, invoking toString() in each object to get a textual representation from it
for (int i = 0; i < objectList.size(); i++) {
Object object = objectList.get(i);
stringArray[i] = object.toString();
}
// Iterate over the string array to print the strigs
for (String string : stringArray) {
System.out.println(string);
}
Upvotes: 0