Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将LinkedList元素与字符串进行比较_Java_String_While Loop_Linked List - Fatal编程技术网

在Java中将LinkedList元素与字符串进行比较

在Java中将LinkedList元素与字符串进行比较,java,string,while-loop,linked-list,Java,String,While Loop,Linked List,我正在尝试编写一个程序,将LinkedList中的字符串与单个字符串进行比较。我试图检查LinkedList中的字符串是否有某个数字:int n在单个字符串中有相同的字母 for (String word : list) { int count = 0; for (int i = 0; i < str.length(); i++) { if (word.contains("" + str.charAt(i))) {

我正在尝试编写一个程序,将LinkedList中的字符串与单个字符串进行比较。我试图检查LinkedList中的字符串是否有某个数字:
int n
在单个字符串中有相同的字母

for (String word : list)
{
   int count = 0; 
   for (int i = 0; i < str.length(); i++)
   {
        if (word.contains("" + str.charAt(i)))
        {
           count ++;
        }
    }
    if (count != n)
    {
        list.remove(word);
    }

   }
}
例如,如果LinkedList中的第一个单词是
“word”
n=2
,单个字符串是
“weed”
。这些单词包含两个相同的字母。所有其他不符合此条件的元素将从LinkedList中删除。这些单词的大小也一样

for (String word : list)
{
   int count = 0; 
   for (int i = 0; i < str.length(); i++)
   {
        if (word.contains("" + str.charAt(i)))
        {
           count ++;
        }
    }
    if (count != n)
    {
        list.remove(word);
    }

   }
}
我已经编写了下面的代码,但我担心这不是实现这一点的最佳方式,否则while循环将无限期地继续下去

    int count = 0;              
    for (String word : list) {
            for (int i = 0; i < str.length(); i++){
                    while (count != n) {
                           if (word.contains("" + str.charAt(i))){
                                count ++;
                            }
                            if (count != n) {
                                list.remove(word);
                            }
                        }
                    }
             }
for (String word : list)
{
   int count = 0; 
   for (int i = 0; i < str.length(); i++)
   {
        if (word.contains("" + str.charAt(i)))
        {
           count ++;
        }
    }
    if (count != n)
    {
        list.remove(word);
    }

   }
}
int count=0;
for(字符串字:列表){
对于(int i=0;i
解决这个问题根本不需要while循环。这个代码应该可以工作

for (String word : list)
{
   int count = 0; 
   for (int i = 0; i < str.length(); i++)
   {
        if (word.contains("" + str.charAt(i)))
        {
           count ++;
        }
    }
    if (count != n)
    {
        list.remove(word);
    }

   }
}
for(字符串字:列表)
{
整数计数=0;
对于(int i=0;i