Object 如何将对象数组转换为字符串数组?

Object 如何将对象数组转换为字符串数组?,object,collections,hashmap,Object,Collections,Hashmap,我已经将hashmap更改为collection,将collection更改为object,现在我需要将object更改为String。无论是这种方式还是任何比这种方式更好的方式,都是非常可观的 Collection<String> t; Collection<String> n; String[] names; String[] types; Object[] foo; Object[] d

我已经将hashmap更改为collection,将collection更改为object,现在我需要将object更改为String。无论是这种方式还是任何比这种方式更好的方式,都是非常可观的

Collection<String> t;
        Collection<String> n;
        String[] names;
        String[] types;
        Object[] foo;
        Object[] doo;
  HashMap<Integer, String> typehashmap=new HashMap<Integer, String>();
           HashMap<Integer, String> namehashmap=new HashMap<Integer, String>();

      t= typehashmap.values();
      n= namehashmap.values();

        foo=t.toArray();
        doo=n.toArray();
      for(int i=0;i<t.size();i++){
 error-->           types[i]=(String) foo[i];
  error-->               names[i]=(String) doo[i];
      }
Collection t;
集合n;
字符串[]名称;
字符串[]类型;
对象[]foo;
对象[]doo;
HashMap typehashmap=新的HashMap();
HashMap namehashmap=新的HashMap();
t=typehashmap.values();
n=namehashmap.values();
foo=t.toArray();
doo=n.toArray();
对于(int i=0;i类型[i]=(字符串)foo[i];
错误-->名称[i]=(字符串)doo[i];
}
只需使用HashMap.values().toArray()例如:

假设“error”是ArrayIndexOutOfBoundsException”,则在向数组添加任何内容之前,需要分配数组。还有另一种toArray方法可以提供帮助:

String[] types = typehashmap.values().toArray(new String[typehashmap.size()]);

这可能是编译器错误!在使用类型数组和名称数组之前,您没有初始化它们。一楼的方法是解决问题的好方法。

如果您还使用它所指的编程语言标记问题,这将非常有帮助。也许您应该告诉我们代码有什么问题;-)运行时错误或编译器错误?这将产生对象数组。仍然存在相同的问题。如何将对象数组转换为字符串数组我已经用这种方法完成了。获得编译时错误。在上面提到的行中。甚至尝试了Casting。我以前也尝试过此操作。可能是我在某个地方出错了。非常感谢。
String[] types = typehashmap.values().toArray(new String[typehashmap.size()]);