why does this run:
static TreeMap<String, int[]> configs = new TreeMap<String, int[]>();
int[] upperarms_body = {2,3,4,6};
int[] left_arm = {1,2};
int[] right_arm = {6,7};
int[] right_side = {5,6,7};
int[] head_sternum = {3,4};
configs.put("upperarms_body", upperarms_body);
configs.put("left_arm", left_arm);
configs.put("right_arm", right_arm);
configs.put("right_side", right_side);
configs.put("head_sternum", head_sternum);
// create a config counter
String[] combi = new String[configs.keySet().size()];
Set<String> s = configs.keySet();
int g = 0;
for(Object str : s){
combi[g] = (String) str;
}
and this not:
static TreeMap<String, int[]> configs = new TreeMap<String, int[]>();
int[] upperarms_body = {2,3,4,6};
int[] left_arm = {1,2};
int[] right_arm = {6,7};
int[] right_side = {5,6,7};
int[] head_sternum = {3,4};
configs.put("upperarms_body", upperarms_body);
configs.put("left_arm", left_arm);
configs.put("right_arm", right_arm);
configs.put("right_side", right_side);
configs.put("head_sternum", head_sternum);
//get an array of thekeys which are strings
String[] combi = (String[]) configs.keySet().toArray();
The method
toArray()returns anObject[]instance, which cannot be cast toString[], just like anObjectinstance cannot be cast toString:However, because you can assign
StringtoObject, you can also putStringintoObject[](which is what probably confuses you):The inverse wouldn’t work, of course
When you do this (from your code):
You’re not actually casting an
Objectinstance toString, you’re casting aStringinstance declared as anObjecttype toString.The solution to your problem would be any of these:
Refer to the Javadoc for more info about
Collection.toArray(T[] a)