搜索ArrayList

-2 java

实现一个方法

public void search (String searchString) { }
Run Code Online (Sandbox Code Playgroud)

迭代注释ArrayList,直到找到包含searchString的注释.然后它应该打印找到的项目或消息"未找到字符串".

到目前为止,我有:

import java.util.ArrayList;
import java.util.Iterator;

/**
 * A class to maintain an arbitrarily long list of notes.
 * Notes are numbered for external reference by a human user.
 * In this version, note numbers start at 0.
 * 
 * @author David J. Barnes and Michael Kolling.
 * @version 2008.03.30
 */
public class Notebook
{
    // Storage for an arbitrary number of notes.
    private ArrayList<String> notes;

    /**
     * Perform any initialization that is required for the
     * notebook.
     */
    public Notebook()
    {
        notes = new ArrayList<String>();
    }

    /**
     * Store a new note into the notebook.
     * @param note The note to be stored.
     */
    public void storeNote(String note)
    {
        notes.add(note);
    }

    /**
     * @return The number of notes currently in the notebook.
     */
    public int numberOfNotes()
    {
        return notes.size();
    }

    /**
     * Show a note.
     * @param noteNumber The number of the note to be shown.
     */
    public void showNote(int noteNumber)
    {
        if(noteNumber < 0) {
            // This is not a valid note number, so do nothing.
            System.out.println("invalid index given");
        }
        else if(noteNumber < numberOfNotes()) {
            // This is a valid note number, so we can print it.
            System.out.println(notes.get(noteNumber));
        }
        else {
             System.out.println("there are fewer items in the notebook");
            // This is not a valid note number, so do nothing.
        }
    }

    public void removeNote(int noteNumber)
    {
        if(noteNumber < 0) {
            // This is not a valid note number, so do nothing.
             System.out.println("invalid index given");
        }
        else if(noteNumber < numberOfNotes()) {
            // This is a valid note number.
            notes.remove(noteNumber);
        }
        else {
            System.out.println("there are fewer items in the notebook");
            // This is not a valid note number, so do nothing.
        }
    }

    /* Edit note.
     * I tried to improve the formatting of the code below, but I'm completely
     * unable to figure out how on earth anything of that should make sense
     * and therefore the indentation is completely without any meaning.
     */
    public void search (String searchString)
    { 
        for each notes in ArrayList {    
            if notes = searchString;        
                System.out.println("String found"); + searchString        
                return    end 
            }
        if}
        System.out.println("String not found");
    }
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用,我无法解决这个问题.

Ano*_*on. 5

几个问题:

  1. 你的search方法实际上在课外.
  2. 搜索方法的主体完全没有任何意义.

如果你依靠写莎士比亚的猴子,你会等待一段时间.