2016-10-06 3 views
-1

코스에 대한 과제를 진행하고 있습니다. 입력 한 int가 내 ArrayList의 유효한 인덱스인지 여부를 확인하는 유효한 인덱스 메소드를 생성하라는 요청을 받았습니다. 이 방법은 잘 컴파일되어 잘 작동합니다.Java 오류 "심볼을 찾을 수 없습니다 - 메소드 (validIndex)"

다른 운동에서는 다른 메서드 내에서이 validIndex 메서드를 사용하도록 요청했습니다. 내 removeFile 메서드 내에서 그렇게하려고했습니다. removeFile 메서드는 removeFile의 index 매개 변수가 ArrayList의 유효한 인덱스인지 여부를 확인하기 위해 validIndex 메서드를 호출하기위한 메서드입니다. 그러나 내 파일은 이제 나에게 오류

을주고 컴파일을 거부

기호를 찾을 수 없습니다 - 방법 validIndex은()

코드는 다음과 같습니다 :

import java.util.ArrayList; 

/** 
* A class to hold details of audio files. 
* 
* @author David J. Barnes and Michael Kölling 
* @version 2011.07.31 
*/ 
public class MusicOrganizer 
{ 
    // An ArrayList for storing the file names of music files. 
    private ArrayList<String> files;  
    /** 
    * Create a MusicOrganizer 
    */ 
    public MusicOrganizer() 
    { 
     files = new ArrayList<String>(); 
    } 

    /** 
    * Add a file to the collection. 
    * @param filename The file to be added. 
    */ 
    public void addFile(String filename) 
    { 
     files.add(filename); 
    } 

    /** 
    * Return the number of files in the collection. 
    * @return The number of files in the collection. 
    */ 
    public int getNumberOfFiles() 
    { 
     return files.size(); 
    } 

    /** 
    * List a file from the collection. 
    * @param index The index of the file to be listed. 
    */ 
    public void listFile(int index) 
    { 
     if(index >= 0 && index < files.size()) { 
      String filename = files.get(index); 
      System.out.println(filename); 
     } 
    } 

    /** 
    * Remove a file from the collection. 
    * @param index The index of the file to be removed. 
    */ 
    public void removeFile(int index) 
    { 
     if(files.validIndex() = true){ 
      files.remove(index); 
     } 
    } 

    // Problem with this method. If ArrayList is empty then the indexes variable returns minus 1 
    public void checkIndex(int index) 
    { 
     int size = files.size(); 
     int indexes = size - 1; 
     if (index >= 0 && index <= indexes){ 
      System.out.println(""); 
     } 
     else { 
      System.out.println("That is not a valid index number."); 
      System.out.println("The index should be between 0 and " + indexes); 
     } 
    } 

    public boolean validIndex(int index) 
    { 

     if (index >= 0 && index <= files.size()-1){ 
      return true; 
     } 
     else { 
      return false; 
     } 
    } 



} 

누군가가 이유를 지적 할 수 있다면 내 코드는 컴파일되지 않을 것입니다.

+1

난 당신이'대신 = TRUE '() files.validIndex'의'validIndex (인덱스)를 사용하는 의미가 생각하는 if 문을 변경합니다. 'files'는이 메소드를 포함하지 않는'List'입니다. – Zircon

+0

int 타입의 매개 변수로'validIndex() '메소드를 구현했습니다. 매개 변수없이이 메서드를 호출하려고합니다. 그것은 큰 차이입니다. 순서를 재정 의하여 방법을 재정의 할 수 있습니다. – reporter

+1

범위를 벗어 났지만 validIndex는 한 줄만 사용할 수 있습니다. 단지'index> = 0 && index <= files.size() - 1'입니다.학습 중이므로 코드 가독성을 향상시키는 데 도움이됩니다. –

답변

1

고유 한 메서드 validIndex를 호출하려는 경우 잘못된 호출입니다.

는 다음을 수행해야합니다, 당신의 validIndex 메서드를 호출 : filesArrayList 클래스의 목적은

public void removeFile(int index) 
    { 
     if(this.validIndex(index)){ 
      files.remove(index); 
     } 
    } 

하는 것으로. 그래서 방법에 문

files.validIndex() 

포인트가 존재하지 않는 ArrayList 클래스에 validIndex()을했다. 이 메서드는 클래스에 있으므로 액세스 할 수있는 유일한 방법은 현재 개체입니다.

if(this.validIndex(...) == true){ ... } 

하거나

if(validIndex(...) == true){ ... } 
+0

감사합니다. 왜 그것이 작동하는지에 대한 간단한 설명을 요청할 수 있습니까? 동일한 클래스 내의 메소드에 대한 메소드 호출은 항상 자바에서이 접두어를 요구합니까? – ed2101

+0

@ ed2101 아니요, '이'를 추가 할 필요가 없습니다. 즉'test'라는 이름의 필드가 있고 method 매개 변수를 test라고하고 매개 변수가 아닌 필드에 액세스하려는 경우 매우 유용합니다. 여기서'this'는 생략 할 수 있습니다. –

0

이 줄.

if(files.validIndex() = true){ 

1) 유효한 색인은 무엇입니까? validIndex 방법이없는 validIndex 방법

2) files에 의해 정의 된대로 그것이 List 객체이기 때문에 당신은, 매개 변수를 사용해야합니다. 메서드가 현재 클래스에 정의되어 있으므로 메서드 호출 앞에 files을 접두어로 사용하지 마십시오. 선택적으로 대체 할 수 있습니다. this

3) 부울 조건이 아닌 대입 연산을 사용하고 있습니다. true를 확인하는 것이 불필요하거나 2 개의 같음을 사용하므로 제거하십시오

So.

if (validIndex(index)) { 
관련 문제