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

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

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 = { &quot;hi,bye&quot; };
mustHaveAt(&#39;h&#39;, 0, loadWords(6, ospd));
mustNotHave(&#39;h&#39;, loadWords(6, ospd));
}
public static List&lt;String&gt; loadWords(int len, String[] ospd) {
List&lt;String&gt; words = new ArrayList&lt;String&gt;(1000);
for (String word : ospd) {
if (word.length() == len) {
words.add(word);
}
}
return words;
}
public static void mustHaveAt(char ch, int position, List&lt;String&gt; aList) {
for (int i = aList.size() - 1; i &gt;= 0; i--) {
String word = aList.get(i);
if (position &gt;= word.length() || word.charAt(position) != ch) {
aList.remove(i);
}
if (position &lt; word.length() &amp;&amp; word.charAt(position) == ch) {
System.out.println(&quot;Word &quot; + word + &quot; has character &quot; + ch + &quot; at position &quot; + position);
}
}
}
public static void mustNotHave(char ch, List&lt;String&gt; aList) {
Iterator&lt;String&gt; itr = aList.iterator();
while (itr.hasNext()) {
String word = itr.next();
if (word.indexOf(ch) &gt;= 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&lt;String&gt; aList) {
for (int i = aList.size() - 1; i &gt;= 0; i--) {
String word = aList.get(i);
if (position &gt;= word.length() || word.charAt(position) != ch) {
aList.remove(i);
}
if (position &lt; word.length() &amp;&amp; word.charAt(position) == ch) {
System.out.println(&quot;Word &quot; + word + &quot; has character &quot; + ch + &quot;at position &quot; + position)
}
}
}

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:

确定