코스에 대한 과제를 진행하고 있습니다. 입력 한 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;
}
}
}
누군가가 이유를 지적 할 수 있다면 내 코드는 컴파일되지 않을 것입니다.
난 당신이'대신 = TRUE '() files.validIndex'의'validIndex (인덱스)를 사용하는 의미가 생각하는 if 문을 변경합니다. 'files'는이 메소드를 포함하지 않는'List'입니다. – Zircon
int 타입의 매개 변수로'validIndex() '메소드를 구현했습니다. 매개 변수없이이 메서드를 호출하려고합니다. 그것은 큰 차이입니다. 순서를 재정 의하여 방법을 재정의 할 수 있습니다. – reporter
범위를 벗어 났지만 validIndex는 한 줄만 사용할 수 있습니다. 단지'index> = 0 && index <= files.size() - 1'입니다.학습 중이므로 코드 가독성을 향상시키는 데 도움이됩니다. –