2014-01-06 3 views
0
나는 다음과 같은 오류를 받고 있어요

발견배열이 필요하지만, java.lang.String의 자바 클래스 배열 오류

array required, but java.lang.String found 

을하고 그 이유를 모르겠어요.

내가하려는 것은 객체의 인스턴스를 넣어 (올바른 용어라고 생각합니다.) 객체의 해당 유형의 배열에 넣는 것입니다.

public class Player{ 
    public Player(int i){ 
      //somecodehere 
    } 
} 

다음 내 주요 방법 나는 그것의 인스턴스를 생성 :

나는 클래스가

static final Player[] a = new Player[5]; // this is where I'm trying to create the array. 
public static void main(String[] args){ 
    Player p = new Player(1); 
    a[0] = p; //this is the line that throws the error 
} 

이 왜 어떤 아이디어? 코드에서

+4

예외를 throw하는 코드가 표시되지 않습니다. 이 코드는 괜찮습니다. –

+0

예외가 발생한 코드를 Google에 제공하십시오! – Rugal

+1

그냥 저장하고 다시 컴파일하십시오. –

답변

4

, 나는 일어나는 그 에러를 볼 수있는 유일한 방법은 당신이 실제로이 경우

static final Player[] a = new Player[5]; // this is where I'm trying to create the array. 
public static void main(String[] args){ 
    String a = "..."; 
    Player p = new Player(1); 
    a[0] = p; //this is the line that throws the error 
} 

가 있다면, 귀하의 지역 변수 a 같은 이름의 static 변수를 그림자 것입니다. 배열 액세스 식

a[0] 

따라서 a 이후

Foo.java:13: error: array required, but String found 
       a[0] = p; // this is the line that throws the error 

같은 컴파일 오류가 발생합니다 것은 배열이 아니라, [] 표기는 배열 형식에 대해 작동합니다.

아마도 저장하고 다시 컴파일하면됩니다.

+0

그것은 실제로 매우 인상적이었습니다. 내 키보드에 문제가 있었고 다른 로컬 변수가있었습니다. 고맙습니다! – jake2389

+1

@ user1530491 어떻게 키보드가이 문제를 일으킬 수 있습니까? :영형 – Baby