0
我需要实现Customer3.toArray()方法。如何将自定义LinkedList转换为对象数组
Customer3是ADT和链接列表节点(它是由老师给我的),我们不允许改变。只需实现toArray()方法。为了简单起见,我已经知道列表中会有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
} //
你被允许使用任何预先定义的库?开始的一个好方法是先尝试找出步骤。你能分享一下你的逻辑吗? – 2013-03-24 03:32:36
提示:现有的方法'show'和'add'具有列表遍历逻辑的变体,这可能在尝试打包数组时有所帮助... – SeKa 2013-03-24 03:38:22