java.lang.ClassCastException: [Ljava.lang.Objet; ne peut pas être jeté à l' [Ljava.lang.Chaîne de caractères;

J'ai besoin de convertir HashMap à un tableau de Chaîne, suivez mon code java

import java.util.HashMap;
import java.util.Map;

public class demo {

    public static void main(String[] args) {

        Map<String, String> map1 = new HashMap<String, String>();

        map1.put("1", "1");
        map1.put("2", "2");
        map1.put("3", "3");

        String[] str = (String[]) map1.keySet().toArray();

        for(int i=0; i<str.length;i++) {
            System.out.println(str[i]);
        }
    }
}

lorsque j'exécute le code, j'ai le ClassCastException.

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
at demo.main(demo.java:17)

OriginalL'auteur JackSun | 2014-03-29