-1

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

  theList.add(p);

変数indexとnは不要になりました。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

2 に答える 2

3

私が最初にすることは、あなたのリスト宣言を変更することです! (あなたが言ったように)

変化する:

private Person[] theList;

private LinkedList<Person> theList;

次に、コンパイラを使用してすべてのコンパイル エラーを出力するか、IDE で生成されたすべての赤い波線を確認します。

コンパイル エラーまたは赤い波線がある各ポイントで、実行しようとしている配列操作を特定します。次に、このページで正しい操作または同等の一連の操作を検索します。http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html

于 2012-05-09T18:39:22.793 に答える
0

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

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

http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html もう少し背景が必要な場合は、このテーマについてさらに読んでください。

于 2012-05-09T18:51:10.377 に答える