【发布时间】:2013-06-14 23:54:10
【问题描述】:
我是一名 Java 初学者,我目前正在从事一个几乎完成的项目。
我需要删除、修改和提取列表的元素(这是一个基本元素,虽然我知道有 arrayList(s)。)这让我自己发疯了,因为我确切地知道我需要做什么,但我我没有得到我应该开始编程的东西。
package lec05;
import java.util.*;
/**
*
* @author ulacit
*/
public class Lista {
Celda head;
public Lista() {
head = null;
}
public void add(Person aPerson) {
if (head == null) { // list = empty
head = new Celda(aPerson);
} else if (aPerson.getId() < head.getInfo().getId()) { // add element - left
Celda aux = new Celda(aPerson);
aux.setNext(head);
head = aux;
} else if (head.getNext() == null) { // add 1 element - right
Celda aux = new Celda(aPerson);
head.setNext(aux);
} else { // more than 1 - add at the end or in the middle
Celda actual = head;
while (actual.getNext() != null
&& actual.getNext().getInfo().getId() < aPerson.getId()) {
actual = actual.getNext();
}
Celda aux = new Celda(aPerson);
aux.setNext(actual.getNext());
actual.setNext(aux);
}
}
public boolean (int id) {
Celda aux = head;
while (aux != null && aux.getInfo().getId() < id) {
aux = aux.getNext();
}
return (aux != null && aux.getInfo().getId() == id);
}
public Person restore(int id) {
Celda aux = head;
while (aux != null && aux.getInfo().getId() < id) {
aux = aux.getNext();
}
if (aux != null && aux.getInfo().getId() == id) {
return aux.getInfo();
} else {
return null;
}
}
public void remove(int id) {
}
public void modify(int id, String name) {
}
public Persona extract(int id) {
}
@Override
public String toString() {
String s = "List{";
Celda aux = head;
while (aux != null) {
s += aux.getInfo() + ", ";
aux = aux.getNext();
}
return s;
}
}
【问题讨论】:
-
你是如何尝试解决这个问题的?
-
看起来您在需要实现的方法中根本没有尝试过任何东西。我们不会为你做作业......
-
另外...我们不知道
Celda类是什么。 -
@jahroy - 但从上下文中看不是很明显吗?不管怎样,谷歌翻译告诉我这是西班牙语:“Lista”是指列表,“Celda”是指细胞。
-
@StephenC - 是的,很容易推断出 Celda 的意思是细胞。我的意思是我们看不到定义他的 Celda 类的代码。不管怎样,你是对的,你的答案是可靠的。