[英]Java getName(String name)
I am extremely new to Java programming only been at it a few weeks and I have this question of traversing a linked list to find a dog with a matching name.我对 Java 编程非常陌生,只接触了几个星期,我有这个问题,即遍历链表以找到具有匹配名称的狗。 I have made an attempt at what I need but I am struggling to understand the traversal.
我已经尝试了我需要的东西,但我很难理解遍历。
Note: I also have a public dog head;
注:我也有
public dog head;
variable at the top.变量在顶部。
Basically what it needs to do is:基本上它需要做的是:
Traverse LinkedList to find a dog with a matching name, if a dog isn't found the return null.遍历 LinkedList 以查找具有匹配名称的狗,如果未找到狗则返回 null。
public Dog getName(String name) {
Dog name = head;
int index;
while(index > 0) {
index--;
name = name.next;
}
if (name == null)
{
return null
}
return name;
}
public Dog getName(String name) {
String dogName = name;
String listCurrentValue = listOfDogNames //Add your list of dog names here
While(listOfDogNames.next()){
If (dogName.equals(listCurrentValue)){
//FOUND MATCH
}
Else
//NO MATCH
}
return name;
}
Like localplutonium says, strings shouldn't be compared with ==, but rather with .equals().就像 localplutonium 所说的,字符串不应该与 == 进行比较,而应该与 .equals() 进行比较。 Also, i see that you use index in your while loop, but you never assign index to a starting value.
另外,我看到您在 while 循环中使用了索引,但您从未将索引分配给起始值。 There is an int variable called index, but it doesn't have a value, so it's impossible to decrement it.
有一个名为 index 的 int 变量,但它没有值,因此无法将其递减。
I found several cosmetic issues in this code: public Dog getName(String name) { // if string name parameter is not used, then why passing it as parameter?我在这段代码中发现了几个外观问题: public Dog getName(String name) { // 如果没有使用字符串名称参数,那么为什么将它作为参数传递? ????
??? Dog name = head;
狗名 = 头; int index;
整数索引;
while(index > 0) {
index--;
name = name.next; //name.next should be name.getNext();
}
// this check is redundant and not need it:
// if null return null else return name???
// so you are in fact returning name value no matter what...
if (name == null)
{
return null
}
return name;
} }
This is homework (obviously) so I can't spoon feed an answer you can hand in, but I can try to create one that is useful and perhaps teaches you something new -- for example the use of collection streams.这是作业(显然)所以我不能用勺子喂你可以提交的答案,但我可以尝试创建一个有用的答案,也许可以教你一些新的东西——例如收集流的使用。
You can traverse a collection using streams and not know or care about how the collection does the traversing, use a filter to select out the item of interest and then return it or null if nothing is found with an expression similar to:您可以使用流遍历集合而不知道或关心集合如何进行遍历,使用过滤器选择出感兴趣的项目,然后返回它,如果没有找到类似的表达式,则返回 null:
public static Dog getDog(String name) {
// given a collection called dogs
return dogs.stream() // get the collection's stream
.filter(d -> d.getName().equals(name)) // filter on the name text
.findFirst() // find the first match and return it
.orElse(null); // or else return null
}
For example:例如:
import java.util.LinkedList;
import java.util.List;
public class TestDog {
private static List<Dog> dogs = new LinkedList<>();
public static void main(String[] args) {
String[] names = { "Fido", "Bingo", "Yeller", "Pluto", "Lassie", "Cujo", "Fang", "Rowf",
"Scamper", "Lady", "Sparky", "Toto", "Bandid", "Beauregard", "Bowser", "Cerberus",
"Daisy", "Duke", "Fred", "Hellhound", "Rex", "Scamp", "Snoopy" };
for (String name : names) {
dogs.add(new Dog(name));
}
System.out.println(getDog("Toto"));
System.out.println(getDog("Meow"));
}
public static Dog getDog(String name) {
return dogs.stream()
.filter(d -> d.getName().equals(name))
.findFirst()
.orElse(null);
}
}
public class Dog {
private String name;
public Dog(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Dog [name=" + name + "]";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.