2012-12-04 5 views
0

개인 int 배열이 있고 어떻게 든 길이를 찾을 필요가 있지만 정적 클래스에서이 작업을 수행 할 수 없습니다. 클래스를 정적으로 유지해야합니다. 길이를 변경하여 추가 할 수있는 또 다른 메소드가 있습니까? 길이가 다른 것입니까?비 정적 필드에서 정적 참조를 변경 하시겠습니까?

문제는 a.length 때문에 발생합니다.

private int [] a; 

public static IntegerSet union(IntegerSet otherSet, IntegerSet nextSet) { 

       for(int i=0; i<a.length; i++) { 
        if (otherSet.isSet(i)) 
        nextSet.insertElement(i); 
       } 

       return nextSet; 
       } 

답변

1

내가

public int lenghtOfArray(){ 
     return this.a.lenght; 
} 

를 사용하고

public static IntegerSet union(IntegerSet otherSet, IntegerSet nextSet) { 
     for(int i=0; i<otherSet.length(); i++) { 
      if (otherSet.isSet(i)) 
       nextSet.insertElement(i); 
      } 
      return nextSet; 
     } 
0

중 하나를 만들 a 정적 또한, 또는 union 방법에 대한 인수로 클래스의 인스턴스를 전달하고 해당 인스턴스의 a 필드에 액세스 할 수 있습니다.

0

당신은 객체의 필드 참조해야합니다

private int [] a; 

public static IntegerSet union(IntegerSet otherSet, IntegerSet nextSet) { 
    . . . 
    for(int i=0; i<otherSet.a.length; i++) { 
     . . . 
    } 
} 
+0

에 방법을 변경 말했듯이 네이 옳았다 – user1874549

0

당신은 정적 클래스에서 a.length를 참조하지 않을을; 당신은 정적 방법에서 그것을하고 있습니다. 정적 메서드는 비 정적 멤버 변수를 참조 할 수 없습니다.

버그가있는 것처럼 보입니다. 왜 a.length에 어울리는 거니? 회원에 대해 otherSet 또는 nextSet에 대해 문의하십시오.

관련 문제