好的,所以搜索是我在Java中的弱点,可以真正使用帮助从哪里开始这个作业!! 不再需要数据成员n。 LinkedList应该在构造函数中创建并分配给List,而不是在run()中。 不应修改makeScanner(),getPerson()和main()。也不应修改Person和FileFormatException类。 display()将不再编译,因为List不再是数组。您可以将其更改为使用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
答案 0 :(得分:3)
我要做的第一件事就是更改你的清单声明! (就像你说的那样)
改变:
private Person[] theList;
到
private LinkedList<Person> theList;
然后使用编译器打印所有编译错误或查看ide中产生的所有红色波形。
在存在编译错误或红色波形的每个点,确定您正在尝试的数组操作。然后在此页面上搜索相同的正确操作或操作顺序。 http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html
答案 1 :(得分:0)
这是使用for for each语句通过简单列表进行链接的示例。您应该将声明从数组更改为链接列表,并尝试类似于上面示例中的每个声明。
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html 如果您需要更多背景知识,请阅读更多有关该主题的内容。