簡體   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