2014-12-29 2 views
-3

나는이 방법으로 프로그램을 만들어야합니다 설정하는 Jugadores에서 주연 :유형의 안전 : 체크되지는 <Jugador>가

private static Set<Jugador>cargarJugadores(String f) throws FileNotFoundException,IOException 
    { 
     ObjectInputStream entrada=null;  

     Set<Jugador>listaDeJugadores=null; 


     //entrada= new ObjectInputStream(new FileInputStream(f)); 
     try 
     { 

      entrada=new ObjectInputStream(new FileInputStream(f)); 


      //Error here 
      listaDeJugadores=(Set<Jugador>) new Jugadores(); 

      byte numDePuntuaciones; 

      while(entrada.readObject()!=null) 
      { 

       listaDeJugadores.add((Jugador)entrada.readObject()); 

       numDePuntuaciones=entrada.readByte(); 

       for(int i=0;i<numDePuntuaciones;i++) 
       { 
        listaDeJugadores.add((Jugador)entrada.readObject()); 
       } 
      } 
     } 
     catch(FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
     catch(ClassNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
     catch(InvalidClassException e) 
     { 
      e.printStackTrace(); 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      try 
      { 
       if(entrada!=null) 
        entrada.close(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
     }  

     return listaDeJugadores; 
    } 

하지만 난이 오류 recived : 나는 시간이하지만 난을 해결하려고했던

Exception in thread "main" java.lang.ClassCastException: Jugadores cannot be cast to java.util.Set 
    at JuegoAsteroides.cargarJugadores(JuegoAsteroides.java:340) 
    at JuegoAsteroides.main(JuegoAsteroides.java:36)

을 그것을 얻지 않았다. 도와주세요. 이

listaDeJugadores=new HashSet<Jugador>();

+5

클래스'Jugadores'는 어디에 있고'Set'을 구현합니까? – RealSkeptic

답변

0

--Error--> listaDeJugadores=(Set) new Jugadores();

수정이 당신은 잘못 JugadorSet<Jugador> A를 캐스팅. 당신이 원하는 listaDeJugadores=(Set<Jugador>) new Jugadores();

을 : : 그 문제를 해결하기 위해, 당신은 당신이 그렇게하는 대신에 ...

Jugador 물건을 올려 Set 구현 무언가의 인스턴스로 listaDeJugadores를 초기화 할 listaDeJugadores = new HashSet<Jugador>();

관련 문제