更改 LinkedList 元素位置
Change LinkedList elements position
我应该完成一个名为“替换”的 class,它可以在它们之间更改 LinkedList 的元素。我一直在尝试自己解决这个问题,但我对编程有点陌生,我找不到答案,如果有人能帮助我,我将不胜感激。提前致谢。
我得到了这段代码,我无法更改,只能在括号之间写上:
import java.util.Iterator;
import java.util.LinkedList;
public class Device implements Iterable<String>{
private static int numDevices=0; //device counter... static atribute
private String name;
private int id;
protected LinkedList<String> words;
public boolean substitute(String word1, String word2) {
//You can't use ListIterator<>
//You must use indexOf()...
//incomplete code that I'm not allowed to change ahead:
int position = this.words.indexOf(word1.toLowerCase());
return true;
}
我还应该通过这个 JUnit5 测试:
assertTrue(d1.substitute("amigo", "perla")); //returns true because the word amigo exists --> returns true
assertFalse(d1.substitute("amigo", "perla")); //the word amigo does not exist --> returns false
assertTrue(d1.substitute("estamos", "estas"));
assertTrue(d1.substitute("que", null)); //remove the word que
assertTrue(d1.substitute("tal", null)); //remove the word tal
Java中的链表Class有方法可以帮你完成这道题。有了在该位置找到的索引,您可以调用 remove() 或 set() 函数来帮助完成您的代码。
public boolean substitute(String word1, String word2) {
int position = this.words.indexOf(word1.toLowerCase());
if(position == -1) {
return false; // index of -1 means the word wasn't found in the list, return false
}
if(word2 == null) { // remove item if word2 is null as indicated by tests
words.remove(position);
} else {
words.set(position, word2); // set word2 at the position word1 was found at
}
return true;
}
我应该完成一个名为“替换”的 class,它可以在它们之间更改 LinkedList 的元素。我一直在尝试自己解决这个问题,但我对编程有点陌生,我找不到答案,如果有人能帮助我,我将不胜感激。提前致谢。
我得到了这段代码,我无法更改,只能在括号之间写上:
import java.util.Iterator;
import java.util.LinkedList;
public class Device implements Iterable<String>{
private static int numDevices=0; //device counter... static atribute
private String name;
private int id;
protected LinkedList<String> words;
public boolean substitute(String word1, String word2) {
//You can't use ListIterator<>
//You must use indexOf()...
//incomplete code that I'm not allowed to change ahead:
int position = this.words.indexOf(word1.toLowerCase());
return true;
}
我还应该通过这个 JUnit5 测试:
assertTrue(d1.substitute("amigo", "perla")); //returns true because the word amigo exists --> returns true
assertFalse(d1.substitute("amigo", "perla")); //the word amigo does not exist --> returns false
assertTrue(d1.substitute("estamos", "estas"));
assertTrue(d1.substitute("que", null)); //remove the word que
assertTrue(d1.substitute("tal", null)); //remove the word tal
Java中的链表Class有方法可以帮你完成这道题。有了在该位置找到的索引,您可以调用 remove() 或 set() 函数来帮助完成您的代码。
public boolean substitute(String word1, String word2) {
int position = this.words.indexOf(word1.toLowerCase());
if(position == -1) {
return false; // index of -1 means the word wasn't found in the list, return false
}
if(word2 == null) { // remove item if word2 is null as indicated by tests
words.remove(position);
} else {
words.set(position, word2); // set word2 at the position word1 was found at
}
return true;
}