2009-12-09 19 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,這樣你可以找到方法和它們的返回值。

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