我有一个学校作业,我需要在 Java 中使用列表并从文本文件中插入单词,并排除类似的单词。我正在尝试这样做,以便仅在不等于现有元素的情况下将新元素添加到列表中,但无论如何它都会不断添加元素。
我在类 List 中按字母顺序插入新元素的方法
public void inserirOrdem( String elemento ) {
No novoNo = new No( elemento );
No actual = cabeca; //actual=head of the list
No anterior = null;
// look for place where to add new element
while( actual != null && actual.item.compareTo( elemento ) < 0 ) {
anterior = actual;
actual = actual.prox;
}
if(!elemento.equals(actual.item)) { //so that the new element won't be added if there's an equal one.... doesn't work
novoNo.prox = actual;
if( anterior == null ) //in case the list doesn't have any elements
cabeca = novoNo;
else
anterior.prox = novoNo;
nElementos++;
}
}
我的节点类:
private class No{
No prox;
String item;
No( String elemento ) {
item = elemento;
prox = null;
}
}
您可以使用 Set 集合。它不允许任何已经等于集合中的新元素。只需要在 No 类中实现 equals 方法。
请参阅此处和此处,但谷歌中有很多示例。
干杯。
这是一个可行的解决方案,尽管我建议进一步测试它。我没有您原来的LList类,所以我使用的是静态版本,该版本应该可以轻松适应您的需求。
class Main {
/* Inserts a node into a unique, alphabetical list */
public static No inserirOrdem(No cabeca, String elemento) {
if (elemento == null) { return cabeca; }
if (cabeca == null) { return new No(elemento); }
No novoNo = new No(elemento);
No actual = cabeca;
No anterior = null;
// valid entry to add at beginning of list
if (cabeca.item.compareTo(elemento) > 0) {
novoNo.prox = cabeca;
return novoNo;
}
while (actual != null && actual.item.compareTo(elemento) < 0) {
anterior = actual;
actual = actual.prox;
}
// valid entry to add at end of list
if (actual == null) {
anterior.prox = novoNo;
return cabeca;
}
// valid entry to add in middle of list
if (actual.item.compareTo(elemento) > 0) {
if (anterior == null) {
cabeca = novoNo;
}
else {
anterior.prox = novoNo;
novoNo.prox = actual;
}
}
return cabeca;
}
/* Prints a linked list */
public static void p(No head) {
No c = head;
while (c != null) {
System.out.print(c.item + " -> ");
c = c.prox;
}
System.out.println("n");
}
public static void main(String[] args) {
No head = new No("a");
No b = new No("b");
No d = new No("d");
No e = new No("e");
head.prox = b;
b.prox = d;
d.prox = e;
System.out.println("Original:");
p(head);
System.out.println("Try to add duplicates:");
head = inserirOrdem(head, "a");
head = inserirOrdem(head, "b");
head = inserirOrdem(head, "d");
p(head);
System.out.println("Try to add c in the middle:");
head = inserirOrdem(head, "c");
p(head);
System.out.println("Try to add f at the end:");
head = inserirOrdem(head, "f");
p(head);
System.out.println("Try to add h at the end:");
head = inserirOrdem(head, "h");
p(head);
System.out.println("Try to add g in the middle:");
head = inserirOrdem(head, "g");
p(head);
head = null;
System.out.println("Null head, add a:");
head = inserirOrdem(head, "a");
p(head);
System.out.println("Try to add null str:");
head = inserirOrdem(head, null);
p(head);
head = new No("c");
System.out.println("Try to add duplicate:");
head = inserirOrdem(head, "c");
p(head);
System.out.println("Try to add a at beginning of list:");
head = inserirOrdem(head, "a");
p(head);
System.out.println("Try to add z at end of list:");
head = inserirOrdem(head, "z");
p(head);
System.out.println("Try to add f at middle of list:");
head = inserirOrdem(head, "f");
p(head);
}
}
class No {
No prox;
String item;
No (String elemento) {
item = elemento;
prox = null;
}
}
输出:
Java 版本 "1.8.0_31"Java(TM( SE 运行时环境(内部版本 1.8.0_31-b13(Java HotSpot(TM( 64-bit Server VM(build 25.31-b07,混合模式(源语言:a -> b -> d -> e ->尝试添加重复项:a -> b -> d -> e ->尝试在中间添加 c:a -> b -> c -> d -> e ->尝试在末尾添加 f:a -> b -> c -> d -> e -> f ->尝试在末尾添加 h:a -> b -> c -> d -> e -> f -> h ->尝试在中间添加 g:a -> b -> c -> d -> e -> f -> g -> h ->空头,添加:a ->尝试添加空 str:a ->尝试添加重复项:c ->尝试在列表开头添加一个:a -> c ->尝试在列表末尾添加 z:a -> c -> z ->尝试在列表中间添加 f:a -> c -> f -> z ->