英文:
I am stuck at this point in learning ArrayList
问题
我正在学习Java中的ArrayList。我正在编写一个猜词的程序。我已经从教程中复制并粘贴了一些代码。但我不知道如何使用它们。程序编译成功。但没有任何输出。以下是代码:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class App {
public static void main(String[] args) throws Exception {
// 以下4行是我自己的代码,试图调用方法。
String[] ospd = { "hi,bye" };
mustHaveAt('h', 0, loadWords(6, ospd));
mustNotHave('h', loadWords(6, ospd));
}
public static List<String> loadWords(int len, String[] ospd) {
List<String> words = new ArrayList<String>(1000);
for (String word : ospd) {
if (word.length() == len) {
words.add(word);
}
}
return words;
}
public static void mustHaveAt(char ch, int position, List<String> aList) {
for (int i = aList.size() - 1; i >= 0; i--) {
String word = aList.get(i);
if (position >= word.length() || word.charAt(position) != ch) {
aList.remove(i);
}
if (position < word.length() && word.charAt(position) == ch) {
System.out.println("Word " + word + " has character " + ch + " at position " + position);
}
}
}
public static void mustNotHave(char ch, List<String> aList) {
Iterator<String> itr = aList.iterator();
while (itr.hasNext()) {
String word = itr.next();
if (word.indexOf(ch) >= 0) {
itr.remove();
}
}
}
}
这是你提供的代码的翻译部分。
英文:
I am learning ArrayList in Java. I am making a program for Guessing a word. I have copied and pasted some code from tutorial. But i dont know how to use them. Program is compiling successfully. But nothing is in output. Here is Code
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class App {
public static void main(String[] args) throws Exception {
// Below 4 lines are mine. Trying to call methods.
String[] ospd = { "hi,bye" };
mustHaveAt('h', 0, loadWords(6, ospd));
mustNotHave('h', loadWords(6, ospd));
}
public static List<String> loadWords(int len, String[] ospd) {
List<String> words = new ArrayList<String>(1000);
for (String word : ospd) {
if (word.length() == len) {
words.add(word);
}
}
return words;
}
public static void mustHaveAt(char ch, int position, List<String> aList) {
for (int i = aList.size() - 1; i >= 0; i--) {
String word = aList.get(i);
if (position >= word.length() || word.charAt(position) != ch) {
aList.remove(i);
}
if (position < word.length() && word.charAt(position) == ch) {
System.out.println("Word " + word + " has character " + ch + " at position " + position);
}
}
}
public static void mustNotHave(char ch, List<String> aList) {
Iterator<String> itr = aList.iterator();
while (itr.hasNext()) {
String word = itr.next();
if (word.indexOf(ch) >= 0) {
itr.remove();
}
}
}
}
答案1
得分: 0
以下是翻译好的部分:
"如果您想检查方法是否成功(猜测单词),请自行添加输出。
您可以这样做:
public static void mustHaveAt(char ch, int position, List<String> aList) {
for (int i = aList.size() - 1; i >= 0; i--) {
String word = aList.get(i);
if (position >= word.length() || word.charAt(position) != ch) {
aList.remove(i);
}
if (position < word.length() && word.charAt(position) == ch) {
System.out.println("Word " + word + " has character " + ch + " at position " + position);
}
}
}
这段代码将为您提供输出,如果一个具有所需字母在所需位置的单词存在。"
英文:
If you want to check if method succeeds (guesses the word) add output yourself.
You can do this:
public static void mustHaveAt(char ch, int position, List<String> aList) {
for (int i = aList.size() - 1; i >= 0; i--) {
String word = aList.get(i);
if (position >= word.length() || word.charAt(position) != ch) {
aList.remove(i);
}
if (position < word.length() && word.charAt(position) == ch) {
System.out.println("Word " + word + " has character " + ch + "at position " + position)
}
}
}
This code will give you output, if a word with desired letter at desired position of a word will be present
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论