2012-05-09 13 views
-1

検索はJavaの私の弱い部分であり、この割り当てをどこから始めるべきかについてのヘルプを実際に使用することができます!! データメンバーnは不要です。 LinkedListを作成して、run()ではなくコンストラクタのListに割り当てる必要があります。 makeScanner()、getPerson()、main()は変更しないでください。 PersonクラスとFileFormatExceptionクラスも変更しないでください。 theListはもはや配列ではないので、display()はコンパイルされなくなります。 foreachを使用するように変更するか、単に削除することができます。 run()には、Personオブジェクトを配列に追加するループがあります。代わりにリストに追加するように変更してください。以下を考慮してください:割り当ての難易度の検索...リンクされたリスト

theList.add(p); 

変数indexとnはもはや必要ありません。 array(配列)ではなくリストに対して線形検索を実行するようにsearch()を変更します。最も簡単な方法は、foreachを使用し、見つかった場合は正しいPersonを返すことです。正しいPersonが見つからない場合は、以前と同じようにnullを返す必要があります。

これは私がこれまで持っているものです:私はどうなる

import java.util.LinkedList; 
import java.io.IOException; 
import java.net.URL; 
import java.util.Scanner; 


public class ContactList { 

private LinkedList<Person> theList; 

private int n;   // the number of Persons in theList 
private Scanner keyboard; 

public ContactList() { 
keyboard = new Scanner(System.in); 
} // no-arg constructor 

// Returns a Scanner associated with a specific text-based URL 
// online. 
private Scanner makeScanner() throws IOException { 
final String source = 
    "http://userpages.umbc.edu/~jmartens/courses/is247/hw/05/05.txt"; 
final URL src = new URL(source); 
return new Scanner(src.openStream()); 
} // makeScanner() 


// Return a Person instance based upon data read from the given 
// Scanner. 
private Person getPerson(final Scanner in) throws FileFormatException { 
if (!in.hasNextLine()) 
    return null; 

String line = in.nextLine().trim(); 
int key = Integer.parseInt(line); 
String name = in.nextLine().trim(); 
String mail = in.nextLine().trim().toLowerCase(); 
if (in.hasNextLine()) { 
    String empty = in.nextLine().trim(); // skip blank line 
    if (empty.length() > 0) 
    throw new FileFormatException("missing blank line"); 
} // if 

return new Person(key, name, mail); 
} // getPerson() 


// Display the array contents. 
private void display() { 
for (int i = 0; i < n; ++i) 
    System.out.println(theList[i]); 
} // display() 


// Read the Person objects from the web page and then start the user 
// interface. 
private void run() throws IOException { 
theList = new Person[1024]; 
try { 
    Scanner in = makeScanner(); 

    int index = 0; 
    Person p = getPerson(in); 
    while (p != null) { 
    theList[index++] = p; 
    p = getPerson(in); 
    } 
    n = index; 
} catch (IOException e) { 
    System.err.println("Error reading web page: " + e); 
    System.exit(1); 
    // The call to exit may be overkill, but it is nice to return an 
    // error (nonzero) value to the environment. Since main() does 
    // nothing after run() returns, simply returning to main() would 
    // be acceptable also. Perhaps the easiest way to do this is to 
    // simply move the call to ui() below into the try block. Then if 
    // an exception is thrown, the UI never executes. 
} // catch 

// Run the user interface. 
    ui(); 
// display(); 
} // run() 

// Loop prompting the user for an integer key. Terminate on a negative 
// key. If a record matching the key is found, display the 
// record. Otherwise, indicate that no matching record was found. 
private void ui() { 
int key = getKey(); 
while (key >= 0) { 
    Person p = search(key); 
    if (p == null) 
    System.out.println("No person matching key " 
         + key 
         + " found."); 
    else 
    System.out.println(p); 
    key = getKey(); 
} // while not done 
} // ui() 

private int getKey() { 
System.out.print("\nPlease enter a key: "); 
int key = keyboard.nextInt(); 
return key; 
} // getKey() 

private Person search(final int key) { 
for (int index = 0; index < n; ++index) 
    if (key == theList[index].getId()) // Is this the right one? 
    return theList[index]; 

return null;  // apparently the requested object is not present 
} // search() 

public static void main(String[] args) throws IOException { 
ContactList cl = new ContactList(); 
cl.run(); 
} // main() 

} // class ContactList 
+4

LinkedListの仕組みを概念的に理解していますか?もしそうでないなら、それは良い出発点です。 – Charles

+0

あなたの質問は何ですか?このコードは具体的な質問に本当に必要なのですか? – amit

答えて

3

最初の事はあなたのリストの宣言を変更しています!そして、すべてのコンパイルエラーを印刷したり、自分のIDEで生産されるすべての赤い波線を見て、あなたのコンパイラを使用

private LinkedList<Person> theList; 

private Person[] theList; 

変更を(あなたのよう言いました)。

コンパイルエラーまたは赤い波打ちがある各ポイントで、どのアレイ操作を試みているかを判断します。次に、このページで、正しい操作または同等の操作のシーケンスを検索します。 http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html

0

http://www.java2s.com/Code/Java/Collections-Data-Structure/Useforeachlooptogothroughelementsinalinkedlist.htm

これは単純なリストをリンクするステートメントごとに使用する例です。上記の例では、宣言を配列からリンクリストに変更し、それぞれに似たものを試してください。

http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html もう少しバックグラウンドが必要な場合は、さらに読んでください。

関連する問題