繁体   English   中英

使用嵌套循环java删除重复项

[英]Remove duplicates with nested loops java

我正在尝试开发一个可以对字符串进行排序并删除重复项的程序。 我为此使用嵌套循环。 但是,当我运行代码时,它只会一遍又一遍地重复几个单词。

package q2;

import java.util.Arrays;

public class Q2 {

public static void main(String[] args) {
    String sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY";
    String lowercaseSentence;
    lowercaseSentence = sentence.toLowerCase();
    String[] sentenceWords = lowercaseSentence.split(" ");
    int LenghtofSentence = sentenceWords.length;
    String[] unique = new String[LenghtofSentence];

    for (int i = 0; i <= LenghtofSentence; i++) {
        //System.out.println(i);
        for (int j = 0; j <= LenghtofSentence; j++) {
            if (!sentenceWords[i].equals(unique)) {
               unique[j] = sentenceWords[i];
               j++;
            } else {
                j++;
            } 
         }
     System.out.println(Arrays.toString(unique));
   }
} 
} 

这是我收到的错误消息:

[ask, null, ask, null, ask, null, ask, null, ask, null, ask, null, ask, null, ask, null, ask]
[not, null, not, null, not, null, not, null, not, null, not, null, not, null, not, null, not]
[what, null, what, null, what, null, what, null, what, null, what, null, what, null, what, null, what]
[your, null, your, null, your, null, your, null, your, null, your, null, your, null, your, null, your]
[country, null, country, null, country, null, country, null, country, null, country, null, country, null, country, null, country]
[can, null, can, null, can, null, can, null, can, null, can, null, can, null, can, null, can]
[do, null, do, null, do, null, do, null, do, null, do, null, do, null, do, null, do]
[for, null, for, null, for, null, for, null, for, null, for, null, for, null, for, null, for]
[you, null, you, null, you, null, you, null, you, null, you, null, you, null, you, null, you]
[ask, null, ask, null, ask, null, ask, null, ask, null, ask, null, ask, null, ask, null, ask]
[what, null, what, null, what, null, what, null, what, null, what, null, what, null, what, null, what]
[you, null, you, null, you, null, you, null, you, null, you, null, you, null, you, null, you]
[can, null, can, null, can, null, can, null, can, null, can, null, can, null, can, null, can]
[do, null, do, null, do, null, do, null, do, null, do, null, do, null, do, null, do]
[for, null, for, null, for, null, for, null, for, null, for, null, for, null, for, null, for]
[your, null, your, null, your, null, your, null, your, null, your, null, your, null, your, null, your]
[country, null, country, null, country, null, country, null, country, null, country, null, country, null, country, null, country]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 17

我正在为此使用Netbeans。 任何帮助表示赞赏。 谢谢基尔

我不知道为什么要为此使用for循环并使它变得复杂。

只需使用Java中的Set即可完成。 Set是一个不包含重复元素的集合。 有关更多链接

Set<String> mySet = new LinkedHashSet<String>(Arrays.asList(sentenceWords));

这将自动删除重复项。 您可以按照以下步骤从Set获取没有重复项的数组:

String[] unique = myset.toArray(new String[myset.size()]);

在使用上述代码之前,还请导入以下内容:

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

使用LinkedHashSet将保持单词在数组中的出现顺序。 希望能帮助到你。

假设您的问题似乎是一种练习,我认为我们不应该给您解决方案,而是建议您解释如何找到解决方案。 首先,如果您可以在练习中使用Java集合,则使用Set<String>可以使您有所改进,因为它将检查单词是否重复,并为您提供一个没有重复的集合。

如果在练习中只能使用数组,则必须使用其他解决方案。 我建议首先对unique数组进行迭代,以检查是否存在重复项,然后对unique数组进行排序。

另一方面,Netbeans使您可以逐步执行代码(如@ f1sh所建议)。

首先,您的逻辑看起来并不完美。

出现ArrayIndexOutOfBound的原因是数组的索引错误,在两个循环中, uniquesentenceWords<=<替换<= ,因为数组的长度是数组中元素的数量,并且索引从0开始。

就像我之前说过的,您应该再考虑一下您的逻辑,因为它并不完美。 您可以使用以下技巧来实现您的目标。

将以下代码替换为您的代码,以删除重复项并对数组进行排序。

String[] unique = Arrays.stream(sentenceWords)
    .distinct().sorted().toArray(String[]::new);

执行此语句后,数组unique包含按词法排序形式的数组sentenceWords单词的不同元素。 有关更多详细信息,请参考javadocs。

 package test;


import java.util.ArrayList;

import java.util.Arrays;


public class Test {


      public static void main(String[] args) {

          String sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN  DO FOR YOUR COUNTRY";

          String lowercaseSentence;

          lowercaseSentence = sentence.toLowerCase();

          String[] sentenceWords = lowercaseSentence.split(" ");

          int LenghtofSentence = sentenceWords.length;

          String[] uniqueString = new String[LenghtofSentence];

           ArrayList<String> unique = new ArrayList<String>();

          int k=0;
          for(int i=0;i<LenghtofSentence;i++)
          {
             if(!unique.contains(sentenceWords[i]))
             {
                unique.add(sentenceWords[i]);
                 k++;
             }
          }
          for(int i=0;i<unique.size();i++)
          {
              uniqueString[i] = unique.get(i);
              System.out.print(" "+uniqueString[i]);
          }
       }
    } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM