我正在尝试按客户对象名称的字母顺序按顺序添加一个新的客户对象(应该是 node.data,而不是节点)。但它不起作用。它以未排序的顺序打印出列表(与原始顺序保持不变)。
public void add(Customer newNode, int dummy){
if (head == null){ // The first node
head = tail = this;
head.setData(newNode);
size=1;
return;
}else{
CustomerList last = null;
for(CustomerList node = head;
node != null && node.getData().toString().compareTo(newNode.name) < 0;
node = node.next){
last = node;
}
CustomerList newList = new CustomerList(newNode);
newList.setNext(last.next);
last.next = newList;
}
} // add
从 txt 文件输入客户对象。应按字母顺序(客户名称)再次打印出来。
10121,Airgo Fresh ods,OH,870023
10125,Bird Out fittered ,MI,870023
10134,Kit river ,IL,870023
10167,Mouin Gontaods,OR,870021
10178,Theiasu El senter,CA,870022
从 txt 文件读取数据并创建对象并添加到列表的代码:
public void byCustomerName()
{
records = null;
System.gc();
CustomerList.setHead(null);
records = new CustomerList();
try
{
String line;
StringTokenizer st;
String id, name, state, salesrep;
BufferedReader infile = new BufferedReader(new FileReader("Customer.txt"));
while ((line = infile.readLine()) != null)
{
st = new StringTokenizer(line, ",");
id = st.nextToken(",");
name = st.nextToken(",");
state = st.nextToken(",");
salesrep = st.nextToken(",");
records.add(new Customer(id, name, state, salesrep), 99);
}
infile.close();
} catch (IOException x) { System.err.println(x); }
} // byCustomerName