【发布时间】:2016-02-16 18:31:04
【问题描述】:
我需要创建一个包含 3 个字段(每个字段)的数组列表,此代码不起作用,需要帮助修复它。这项任务似乎很简单,但我无法弄清楚出了什么问题。我正在运行 netbeans
这是我目前所拥有的
import java.util.Scanner;
import java.util.ArrayList;
public class scientist1
{
public static void main (String[] args)
{
Scanner kbd1 = new Scanner(System.in);
ArrayList<Document> someStuff = new ArrayList<Document>();
char quit = 'Y';
String scientist, field, greatIdea;
while (quit == 'Y')
{
System.out.print("\n Scientists name: ");
scientist = scan.next();
System.out.print(" scientists field: ");
field = scan.next();
System.out.print(" Scientists great Ideas: ");
greatIdea = scan.nextInt();
someStuff.add (new Document(field, scientist, greatIdea));
System.out.print(" Enter Another Scientist? (Y/N)");
String word = scan.next();
word = word.toUpperCase();
quit= word.charAt(0);
}
for(Document stuff : someStuff)
System.out.println(stuff);
}
}
public class Document
{
public static String scientist, field;
private int greatIdea;
public Document (String Last, String First,String Idea)
{
scientist = First;
field = Last;
greatIdea = Idea;
}
public String toString ()
{
return "\n\n Name: " + field + ", " + scientist + "\n Document Code: " + greatIdea + "\n";
}
public boolean equals (Object other)
{
return (field.equals(((Document)other).getLast())&&
scientist.equals(((Document)other).getFirst()));
}
public int compareTo (Object other)
{
int result;
String otherFirst = ((Document)other).getFirst();
String otherLast = ((Document)other).getLast();
if (field.equals(otherLast))
result = scientist.compareTo(otherFirst);
else
result = field.compareTo(otherLast);
return result;
}
public String getFirst ()
{
return scientist;
}
public String getLast ()
{
return field;
}
}
【问题讨论】:
-
我想你不应该在这里有
static->public static String scientist, field; -
是否很快必须成为冒泡排序?
-
不,它不必是冒泡排序。我认为这是最简单的。
-
@Domisaur 好的。我可以澄清一下你想按科学家的名字提取文件的排序吗?
-
是的,我想通过在程序中搜索科学家姓名来提取与科学家姓名相关的数据。我的主要问题是现在虽然@Dan 得到了包含 3 个部分的数组列表