好的,所以搜索是我在 Java 中的弱点,真的可以帮忙从哪里开始这个作业!!不再需要数据成员 n。应该在构造函数中创建 LinkedList 并将其分配给 List,而不是在 run() 中。不应修改 makeScanner()、getPerson() 和 main()。也不应修改 Person 和 FileFormatException 类。display() 将不再编译,因为 List 不再是一个数组。您可以将其更改为使用 foreach 或直接将其删除。run() 有一个将 Person 对象添加到数组中的循环。更改它,以便将它们添加到列表中。考虑:
theList.add(p);
不再需要变量 index 和 n。修改 search() 以对列表而不是数组执行线性搜索。最简单的方法是使用 foreach 并在找到时返回正确的 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
我要做的第一件事就是更改您的列表声明!(就像你说的)
改变:
private Person[] theList;
自
private LinkedList<Person> theList;
然后使用编译器打印所有编译错误或查看 ide 中生成的所有红色波浪线。
在出现编译错误或红色波浪线的每个点,确定您正在尝试的数组操作。然后在此页面上搜索正确的操作或等效操作序列。http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html
http://www.java2s.com/Code/Java/Collections-Data-Structure/Useforeachlooptogothroughelementsinalinkedlist.htm
这是使用 for each 语句链接简单列表的示例。 您应该将声明从数组更改为链表,并尝试类似于上述示例中每个 for 的操作。
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html如果您需要更多背景知识,请阅读更多有关该主题的内容。