英文:
Parameters in startsWith(String word, char c)
问题
我正在努力理解我收到的一项备考任务,为了准备我在几天后要参加的考试。以下是我目前所写的代码:
```java
/**
* 检查单词的首字母是否与给定的字母 c 匹配
* @param word 一个单词
* @param c - 一个字符 a-z 大写或小写
* @return 如果单词的首字母与 c 匹配,则返回 true
*/
@Override
public boolean startsWith(String word, char c) {
}
我应该编写代码以满足在另一个文件中给出的测试要求。所有的测试代码都已经给我了,我唯一的任务就是编写代码,如果字符串 word 的首字母与字符 c 匹配,则返回 true。无论字母是大写还是小写,测试代码文件中都已经包含了处理这个问题的代码。
总结一下:
我想要问的是,我究竟需要做什么?我已经尝试过类似以下的代码,但我认为这是不正确的,而且我完全忽视和忽略了任务的一个重要部分:
@Override
public boolean startsWith(String word, char c) {
if (word.startsWith(word, c)) {
return true;
} else {
return false;
}
}
<details>
<summary>英文:</summary>
I am struggling with understanding a practice task that I have received in preparation for a test I have in a few days. This is the code that I have so far:
/**
- Check if the first letter in this word matches the given letter c
- @param word a word
- @param c - a character a-z upper or lower case
- @return true if first letter in word matches c
*/
@Override
public boolean startsWith(String word, char c) {
}
I am supposed to write code that will pass the test requirements that I have in a separate file. All of the test code has already been given to me, and my only task is to write code that returns true if the first letter in String word matches char c. It does not matter if the letter is in uppercase or if it is in lowercase as the file with the test code contains code that handles this problem.
To summarize:
I guess what I am asking for is help in understanding what exactly I'm supposed to do? I have tried writing something along the lines of this, but I do not think this is correct and I think I am completely ignoring and neglecting a huge part of the task:
@Override
public boolean startsWith(String word, char c) {
if (word.startsWith(word, c)) {
return true;
} else {
return false;
}
}
</details>
# 答案1
**得分**: 2
问题在于你误解了[`startsWith()`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith(java.lang.String,%20int))方法的参数。第一个参数是前缀。这应该是你的字符,即`c`。第二个参数是开始查找该前缀的偏移量。在这种情况下,你希要该偏移量为0。你真正想要的是 `word.startsWith(Character.toString(c), 0)`。你的原始代码之所以没有引发编译时异常,是因为字符`c`只是被当作数字处理,并且为了匹配`startsWith()`方法的方法头,它被上转型为int类型。
<details>
<summary>英文:</summary>
The issue here is that you've misunderstood the arguments to the [`startsWith()`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith(java.lang.String,%20int)) method. The first argument is the prefix. That should be your character, `c`. The second argument is the offset to start looking for that prefix at. In this case, you want that to be 0. What you really want is `word.startsWith(Character.toString(c), 0)`. The reason your original code didn't throw a compile-time exception is because the character `c` was just being treated like a number, and it was being upcasted to an int in order to match the method header of the `startsWith()` method.
</details>
# 答案2
**得分**: 1
这对你有很大的好处,学会在你使用的Java标准库版本中查找JavaDocs的位置。Java 10的文档在这里:https://docs.oracle.com/javase/10/docs/api/index.html?overview-summary.html,其他几个版本的文档也在附近。你可以通过这些文档查找感兴趣的类,比如[`java.lang.String`][1],以确定它们提供了哪些方法,并了解这些方法的参数和行为的详细信息。
特别地,你可以考虑查看`String`的几个`startsWith()`方法,这些方法可以确定被调用的`String`对象是否以特定字符串或与特定模式匹配的字符串开头。这与你尝试使用匹配你调用参数的那个方法不同。
另一个回答描述了如何在你的问题中应用`String.startsWith()`,但这并不是你唯一的选择。浏览可用的方法会找到从`String`中获取单个`char`的几种方法,比如`charAt()`方法。如果确实不需要考虑大小写(这对我来说听起来很奇怪),那么我个人会倾向于提取第一个字符(索引为0)并将其与所需字符进行比较。也就是说,我会从这里开始:
char firstChar = word.charAt(0);
其余部分留作练习。
[1]: https://docs.oracle.com/javase/10/docs/api/java/lang/String.html
<details>
<summary>英文:</summary>
It would be much to your advantage to learn where to find the JavaDocs for the version of the Java Standard Library you are using. Those for Java 10 are here: https://docs.oracle.com/javase/10/docs/api/index.html?overview-summary.html, and those for several other versions are nearby. Among the things you can do with those is look up classes of interest, such as [`java.lang.String`][1], to determine what methods they provide and to learn details of those methods' arguments and behavior.
In particular, you might consider checking `String`'s several `startsWith()` methods, in which case you would find that they determine whether the `String` object *on which they are invoked* starts with a specific string or a string matching a specific pattern. That's not how you are trying to use the one that matches your invocation's arguments.
Another answer describes you you might apply `String.startsWith()` to your problem, but that's not your only option. Perusing the available methods would turn up several ways to get individual `char`s from the `String`, such as the `charAt()` method. If indeed you don't need to worry about case (which sounds surprising to me) then, personally, I would be inclined to extract the first character (at index 0) and compare that to the character of interest. That is, I would start here:
char firstChar = word.charAt(0);
The rest is left as an exercise.
[1]: https://docs.oracle.com/javase/10/docs/api/java/lang/String.html
</details>
# 答案3
**得分**: 0
```java
string startsWith方法文档来自[oracle][1]:
> startsWith(String prefix, int offset)
> 测试从指定索引开始的此字符串子字符串是否以指定前缀开头。
[1]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
但您需要测试第一个字符是否以字符c开头。因此,您可以使用startsWith(String word)的重载版本(来自String)。以下是我的两个选项:
选项1:
@Override
public boolean startsWith(String word, char c) {
return word.charAt(0) == c;
}
选项2:
@Override
public boolean startsWith(String word, char c) {
return word.startsWith(String.valueOf(c));
}
请注意,您可以通过检查答案中提到的"word"参数是否为null来改进这个方法。例如:
@Override
public boolean startsWith(String word, char c) {
return word != null && word.startsWith(String.valueOf(c));
}
英文:
string startsWith method documentation from oracle:
> startsWith(String prefix, int offset)
> Tests if the substring of this
> string beginning at the specified index starts with the specified
> prefix.
But you need to test whether the first character starts with character c. So you can use the overloaded version of startsWith(String word) (from String). here is my 2 options:
option 1:
@Override
public boolean startsWith(String word, char c) {
return word.charAt(0) == c;
}
option 2:
@Override
public boolean startsWith(String word, char c) {
return word.startsWith(String.valueOf(c);
}
Note that you can improve this method by checking the "word" argument nullity as mentioned in answers. for example:
@Override
public boolean startsWith(String word, char c) {
return word != null && word.startsWith(String.valueOf(c);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论