-1

好的,所以搜索是我在 Java 中的薄弱环节,并且可以真正使用帮助来确定从哪里开始这个作业!不再需要数据成员 n。应该在构造函数中而不是在 run() 中创建 LinkedList 并将其分配给 theList。不应修改 makeScanner()、getPerson() 和 main()。Person 和 FileFormatException 类也不应该被修改。display() 将不再编译,因为 theList 不再是一个数组。您可以将其更改为使用 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

这是一个使用 for each 语句链接一个简单列表的示例。您应该将声明从数组更改为链表,并尝试与上述示例中的 for each 类似的方法。

http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html 如果您需要更多背景知识,请阅读有关该主题的更多内容。

于 2012-05-09T18:51:10.377 回答