我需要实现方法 Customer3.toArray()。
Customer3 是 ADT 和链表节点(这是老师给我的),我们不允许更改。只需实现 toArray() 方法。为简单起见,我已经知道 List 中将有 64 个客户对象,因此我创建了一个包含 64 个元素的数组。
我不确定如何为 LinkedLists 执行此操作。我希望也许有人可以向我展示一种简单的转换方法。
代码:
public class
Customer3
implements java.io.Serializable
{
Customer3 next;
String ID;
String name;
String state;
String salesID;
public Customer3() {
ID = null; name = null; state = null; salesID = null;
next = null;
}
public Customer3(String i, String n, String s, String si){
ID = i;
name = n;
state = s;
salesID = si;
next = null;
}
public Customer3 getNext() { return next; }
public String getID() { return ID; }
public String getName() { return name; }
public String getState() { return state; }
public String getSalesID() { return salesID; }
public void setNext(Customer3 n) { next = n; }
public void setName(String n) { name = n; }
public void setState(String s) { state = s; }
public void setSalesID (String si) { salesID = si; }
public String toString() { return ID + " " + name + " " + state + " " + salesID; }
public Customer3 add(Customer3 h) {
if (h == null) return this;
Customer3 temp = h;
while (temp.next != null) // look for the end of the list
temp = temp.next;
temp.next = this; // append the new node to the end
return h;
} // add
public void show() {
System.out.println(this);
if (next != null) next.show();
} // show
public Object [] toArray()
{
Object [] temp=null;
temp = new Object[64];
return temp;
} // toArray
} //