【发布时间】:2017-06-29 10:30:40
【问题描述】:
大家好,我是 java 新手,所以非常感谢您对此提供的任何帮助。 好的,这是问题所在: 我有一个列表类和一个 listNode 类, 列表 Class 由名称、firstNode 和 lastNode 表示。 firstNode 和 lastNode 来自 listNode 类型, listNode 由一个 Object(例如 data 或 Object o)和一个 nextNode 表示,它指向列表中的下一个节点,该节点也来自 listNode 类型。
列表类:
public class List {
private ListNode firstNode;
private ListNode lastNode;
private String name;
public List() {
this("list");
}
public List(String listName) {
name = listName;
firstNode = lastNode = null;
}
public void insertAtFront(Object insertItem) {
if (isEmpty())
firstNode = lastNode = new ListNode(insertItem);
else
firstNode = new ListNode(insertItem, firstNode);
}
public void insertAtBack(Object insertItem) {
if (isEmpty())
firstNode = lastNode = new ListNode(insertItem);
else
lastNode = lastNode.nextNode = new ListNode(insertItem);
}
public Object removeFromFront() throws EmptyListException {
if (isEmpty())
throw new EmptyListException(name);
Object removedItem = firstNode.data;
if (firstNode == lastNode)
firstNode = lastNode = null;
else
firstNode = firstNode.nextNode;
return removedItem;
}
public Object removeFromBack() throws EmptyListException {
if (isEmpty())
throw new EmptyListException(name);
Object removedItem = lastNode.data;
if (firstNode == lastNode)
firstNode = lastNode = null;
else {
ListNode current = firstNode;
while (current.nextNode != lastNode)
current = current.nextNode;
lastNode = current;
current.nextNode = null;
}
return removedItem;
}
public boolean isEmpty() {
return firstNode == null;
}
public void print() {
if (isEmpty()) {
System.out.printf("Empty %s\n", name);
return;
}
System.out.printf("The %s is : ", name);
ListNode current = firstNode;
while (current != null) {
System.out.printf("%s", current.data);
current = current.nextNode;
}
System.out.println("\n");
}
@Override
public String toString() {
String stk = "(";
if(isEmpty())return "Empty List";
ListNode checkNode = firstNode;
while (checkNode != null) {
stk += checkNode.data.toString()+ " , ";
checkNode = checkNode.nextNode;
}
return stk+")";
}
public ListNode removeAt (int k){
if(k<=0 || k>getLength())
try{
throw new IllegalValues();
}catch(IllegalValues iv){
iv.printStackTrace();
return null;
}
ListNode newNode = firstNode;
if(k==1){
newNode = firstNode;
firstNode = firstNode.nextNode;
return newNode;
}
if(k==2){
newNode = firstNode.nextNode;
firstNode.nextNode = firstNode.nextNode.nextNode;
return newNode;
}
if(k==3){
newNode = firstNode.nextNode;
firstNode.nextNode.nextNode = firstNode.nextNode.nextNode.nextNode;
return newNode;
}
if(k==4){
newNode = firstNode.nextNode;
firstNode.nextNode.nextNode.nextNode = firstNode.nextNode.nextNode.nextNode.nextNode;
return newNode;
}
return newNode;
}
public int getLength(){
ListNode checkNode = firstNode;
int count =0;
while (checkNode != null) {
count++;
checkNode = checkNode.nextNode;
}
return count;
}
}
listNode 类:
public class ListNode {
Object data;
ListNode nextNode;
public ListNode(Object o) {
this(o, null);
}
public ListNode(Object o, ListNode node) {
data = o;
nextNode = node;
}
public Object getObject() {
return data;
}
public ListNode getNext(){
return nextNode;
}
}
所以这是我正在使用的两个类。 我的问题是 removeAt() 方法 我不知道如何概括它并为所有代码(如 for 语句)做出一般回答我只能通过编写每个案例来使其工作在 if 语句中分开。我需要编写一个 for 循环,以某种方式循环抛出 hasNext() 方法。 有任何想法吗?? 提前致谢
【问题讨论】:
-
您确定要以 1 开始索引吗?在 Java 中,索引从 0 开始,因此您的 List 实现可能会让人感到困惑。
标签: java eclipse list collections