2009-12-09 16 views
0

这是迄今为止的内容,但我现在不知道接下来要做什么。现在的问题是如下(遗憾的编码是不是都在一个盒子里出现): 实现方法搜索字符串的ArrayList以查找文本

public void search (String searchString) { } 

通过Notes的ArrayList迭代,直到它找到一个包含搜索字符串的说明。然后它应该打印找到的项目或消息“未找到字符串”。在测试时检查列表中的字符串和不是的字符串。

代码:新for-each style loops

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 than that"); 
     // 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 than that"); 
     // This is not a valid note number, so do nothing. 
    } 
} 

public void multiplesOfFive() 
{ 

    int i = 10; 
    while(i < 100) 
    { 
     System.out.println(i); 
     i = i + 5; 
    } 
} 

public int sum(int a, int b) 
{ 

    int index = a; 
    int result = 0; 
    while(index <= b) 
    { 
     result = result + index; 
     index = index + 1; 
    } 
    return result; 
} 

public int product(int a, int b) 
{ 

    int index = a; 
    int result = 1; 
    while(index <= b) 
    { 
     result = result * index; 
     index = index + 1; 
    } 
    return result; 
} 

public boolean 
    isPrime (int n) 
    { 
     if (n<=1)return false; 
     if (n==2) return true; 
     for (int i = 2;i<=n-1;i++) 
     { 
     if (n%i==0)return false; 
     } 
     return true; 
    } 
} 
+0

什么是总和,产品,isPrime等? – 2009-12-09 23:40:53

+0

Notebook类很可能是指导者提供的基础,并且将执行一系列分配 – akf 2009-12-10 00:06:05

回答

0

利用一个遍历的笔记列表:

for (String string : notes) { 
    // This will loop over all the Strings in the notes List. 
    // Perform your logic here. 
} 
1

两个思路来考虑:

  1. 当您撰写搜索方法,请在迭代时考虑在String类中使用contains方法(请参阅Kaleb Brasee's后)。
  2. 确保您处理传递null作为搜索参数的情况。
0

如果列表不是按字母顺序排列,则需要遍历列表,将每个字符串与搜索字符串进行比较。一旦你找到了一个匹配,你可以打破循环(使用return true(或字符串)将是最简单的方法),那么在循环外部,你可以返回false来表示找不到匹配。

您将需要使用一些方法:
的ArrayList

  • 大小() - 为您提供了列表的大小,所以你知道,当你走到了尽头
  • get(int index) - 返回列表中指定索引处的项目

字符串:

  • 等于(字符串CMP) - 比较两个字符串并返回一个int

这将是很好的熟悉的Java API,这样你可以找到方法和它们的返回值。

如果列表按字母顺序排列,则有更高效的搜索方式。