Java 错误 "Cannot find symbol - method(validIndex)"

Java error "Cannot find symbol - method(validIndex)"

我正在完成课程作业。我被要求创建一个有效的索引方法来检查输入的 int 是否是我的 ArrayList 的有效索引。这种方法编译得很好并且工作得很好。

进一步的练习要求我在其他方法中使用此 validIndex 方法。我试图在我的 removeFile 方法中做到这一点。 removeFile 方法旨在调用 validIndex 方法以检查 removeFile 的索引参数是否是 ArrayList 的有效索引。但是我的文件现在拒绝编译给我错误

cannot find symbol - method 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;
        }
    }



}

如果有人能指出我的代码无法编译的原因,我们将不胜感激。

这一行。

if(files.validIndex() = true){

1) 什么索引是有效索引?您需要使用 validIndex 方法定义的参数

2) files 没有 validIndex 方法,因为它是一个 List 对象。您的方法在您当前的 class 中定义,因此不要在方法调用前加上 files。您可以选择将其替换为 this

3) 您正在使用赋值运算,而不是布尔条件。要么删除它,因为检查 true 是多余的,要么使用两个等于

所以。

if (validIndex(index)) {

如果您试图调用自己的方法 validIndex,那您就错了。

要调用您的 validIndex 方法,您应该执行以下操作:

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

请注意 filesArrayList class 的对象。所以语句

files.validIndex()

指向 ArrayList class 中名为 validIndex() 的方法,该方法不存在。该方法存在于您的 class 中,因此访问它的唯一方法是使用当前对象。将 if 语句更改为

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

或者干脆

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