Java 搜索作业难度。。。链表

Java 搜索作业难度。。。链表,java,search,linked-list,Java,Search,Linked List,好的,所以搜索是我在Java中的薄弱环节,对于从何处开始这项任务,我真的需要帮助!! 不再需要数据成员n。 应在构造函数中而不是在run()中创建LinkedList并将其分配给该列表。 不应修改makeScanner()、getPerson()和main()。Person和FileFormatException类也不应修改。 display()将不再编译,因为列表不再是数组。您可以将其更改为使用foreach,也可以将其删除。 run()有一个将Person对象添加到数组的循环。将其更改为将这

好的,所以搜索是我在Java中的薄弱环节,对于从何处开始这项任务,我真的需要帮助!! 不再需要数据成员n。 应在构造函数中而不是在run()中创建LinkedList并将其分配给该列表。 不应修改makeScanner()、getPerson()和main()。Person和FileFormatException类也不应修改。 display()将不再编译,因为列表不再是数组。您可以将其更改为使用foreach,也可以将其删除。 run()有一个将Person对象添加到数组的循环。将其更改为将这些内容添加到列表中。考虑:

  theList.add(p);
变量index和n不再是必需的。 Modify search()对列表而不是数组执行线性搜索。最简单的方法是使用foreach并在找到正确的人时返回。如果找不到正确的人员,它应该像以前一样返回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
import java.util.LinkedList;
导入java.io.IOException;
导入java.net.URL;
导入java.util.Scanner;
公共类联系人列表{
私有链接列表;
private int n;//列表中的人数
专用扫描仪键盘;
公共联系人列表(){
键盘=新扫描仪(System.in);
}//没有参数构造函数
//返回与特定基于文本的URL关联的扫描程序
//在线。
专用扫描程序makeScanner()引发IOException{
最终字符串源=
"http://userpages.umbc.edu/~jmartens/courses/is247/hw/05/05.txt”;
最终URL src=新URL(源);
返回新扫描仪(src.openStream());
}//makeScanner()
//根据从给定数据库读取的数据返回Person实例
//扫描仪。
private Person getPerson(中的最终扫描程序)引发FileFormatException{
如果(!in.hasNextLine())
返回null;
字符串行=in.nextLine().trim();
int key=Integer.parseInt(行);
字符串名称=in.nextLine().trim();
字符串mail=in.nextLine().trim().toLowerCase();
if(在.hasNextLine()中){
String empty=in.nextLine().trim();//跳过空行
if(empty.length()>0)
抛出新的FileFormatException(“缺少空行”);
}//如果
返回新人员(密钥、姓名、邮件);
}//getPerson()
//显示数组内容。
专用void display(){
对于(int i=0;i=0){
人员p=搜索(键);
if(p==null)
System.out.println(“无人匹配密钥”
+钥匙
+“找到了。”);
其他的
系统输出println(p);
key=getKey();
}//虽然没有完成
}//ui()
私有int getKey(){
System.out.print(“\n请输入一个键:”);
int key=keyboard.nextInt();
返回键;
}//getKey()
私人搜索(最终整数键){
对于(int-index=0;index
我要做的第一件事就是更改您的列表声明!(就像你说的)

更改:

private Person[] theList;

私有链接列表;
然后使用编译器打印所有编译错误或查看ide中生成的所有红色曲线

在出现编译错误或红色扭曲的每个点上,确定要尝试的数组操作。然后在此页面上搜索相应的正确操作或操作顺序

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


如果您需要更多的背景知识,请多阅读相关内容。

您是否了解LinkedList的概念工作原理?如果不是,那是个好的开始。你的问题是什么?具体问题真的需要这些代码吗?
private LinkedList<Person> theList;