我在学习ArrayList时遇到了困难。

huangapple go评论99阅读模式
英文:

I am stuck at this point in learning ArrayList

问题

我正在学习Java中的ArrayList。我正在编写一个猜词的程序。我已经从教程中复制并粘贴了一些代码。但我不知道如何使用它们。程序编译成功。但没有任何输出。以下是代码

  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. public class App {
  5. public static void main(String[] args) throws Exception {
  6. // 以下4行是我自己的代码,试图调用方法。
  7. String[] ospd = { "hi,bye" };
  8. mustHaveAt('h', 0, loadWords(6, ospd));
  9. mustNotHave('h', loadWords(6, ospd));
  10. }
  11. public static List<String> loadWords(int len, String[] ospd) {
  12. List<String> words = new ArrayList<String>(1000);
  13. for (String word : ospd) {
  14. if (word.length() == len) {
  15. words.add(word);
  16. }
  17. }
  18. return words;
  19. }
  20. public static void mustHaveAt(char ch, int position, List<String> aList) {
  21. for (int i = aList.size() - 1; i >= 0; i--) {
  22. String word = aList.get(i);
  23. if (position >= word.length() || word.charAt(position) != ch) {
  24. aList.remove(i);
  25. }
  26. if (position < word.length() && word.charAt(position) == ch) {
  27. System.out.println("Word " + word + " has character " + ch + " at position " + position);
  28. }
  29. }
  30. }
  31. public static void mustNotHave(char ch, List<String> aList) {
  32. Iterator<String> itr = aList.iterator();
  33. while (itr.hasNext()) {
  34. String word = itr.next();
  35. if (word.indexOf(ch) >= 0) {
  36. itr.remove();
  37. }
  38. }
  39. }
  40. }

这是你提供的代码的翻译部分。

英文:

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

  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. public class App {
  5. public static void main(String[] args) throws Exception {
  6. // Below 4 lines are mine. Trying to call methods.
  7. String[] ospd = { &quot;hi,bye&quot; };
  8. mustHaveAt(&#39;h&#39;, 0, loadWords(6, ospd));
  9. mustNotHave(&#39;h&#39;, loadWords(6, ospd));
  10. }
  11. public static List&lt;String&gt; loadWords(int len, String[] ospd) {
  12. List&lt;String&gt; words = new ArrayList&lt;String&gt;(1000);
  13. for (String word : ospd) {
  14. if (word.length() == len) {
  15. words.add(word);
  16. }
  17. }
  18. return words;
  19. }
  20. public static void mustHaveAt(char ch, int position, List&lt;String&gt; aList) {
  21. for (int i = aList.size() - 1; i &gt;= 0; i--) {
  22. String word = aList.get(i);
  23. if (position &gt;= word.length() || word.charAt(position) != ch) {
  24. aList.remove(i);
  25. }
  26. if (position &lt; word.length() &amp;&amp; word.charAt(position) == ch) {
  27. System.out.println(&quot;Word &quot; + word + &quot; has character &quot; + ch + &quot; at position &quot; + position);
  28. }
  29. }
  30. }
  31. public static void mustNotHave(char ch, List&lt;String&gt; aList) {
  32. Iterator&lt;String&gt; itr = aList.iterator();
  33. while (itr.hasNext()) {
  34. String word = itr.next();
  35. if (word.indexOf(ch) &gt;= 0) {
  36. itr.remove();
  37. }
  38. }
  39. }
  40. }

答案1

得分: 0

以下是翻译好的部分:

"如果您想检查方法是否成功(猜测单词),请自行添加输出。

您可以这样做:

  1. public static void mustHaveAt(char ch, int position, List<String> aList) {
  2. for (int i = aList.size() - 1; i >= 0; i--) {
  3. String word = aList.get(i);
  4. if (position >= word.length() || word.charAt(position) != ch) {
  5. aList.remove(i);
  6. }
  7. if (position < word.length() && word.charAt(position) == ch) {
  8. System.out.println("Word " + word + " has character " + ch + " at position " + position);
  9. }
  10. }
  11. }

这段代码将为您提供输出,如果一个具有所需字母在所需位置的单词存在。"

英文:

If you want to check if method succeeds (guesses the word) add output yourself.

You can do this:

  1. public static void mustHaveAt(char ch, int position, List&lt;String&gt; aList) {
  2. for (int i = aList.size() - 1; i &gt;= 0; i--) {
  3. String word = aList.get(i);
  4. if (position &gt;= word.length() || word.charAt(position) != ch) {
  5. aList.remove(i);
  6. }
  7. if (position &lt; word.length() &amp;&amp; word.charAt(position) == ch) {
  8. System.out.println(&quot;Word &quot; + word + &quot; has character &quot; + ch + &quot;at position &quot; + position)
  9. }
  10. }
  11. }

This code will give you output, if a word with desired letter at desired position of a word will be present

huangapple
  • 本文由 发表于 2020年8月4日 19:50:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63246286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定