配列内の重複値を確認してください
ここで、配列内の重複した値をチェックする方法を示すために、Javaの例を添付しました。 これを実装する2つの方法を示します。
1)2つのforループを使用して配列の各値を比較する–21行目
2)HashSetを使用して、重複した値を検出します。 –行40
配列内の重複値を比較する他の方法を知っている場合は、助けてほしい、私にそれを共有してください。
package com.example; import java.util.Set; import java.util.HashSet; public class CheckDuplicate { public static void main(String args[]) { String [] sValue = new String[]{"a","b","c","d","","","e","a"}; if(checkDuplicated_withNormal(sValue)) System.out.println("Check Normal : Value duplicated! \n"); if(checkDuplicated_withSet(sValue)) System.out.println("Check Set : Value duplicated! \n"); } //check duplicated value private static boolean checkDuplicated_withNormal(String[] sValueTemp) { for (int i = 0; i < sValueTemp.length; i++) { String sValueToCheck = sValueTemp[i]; if(sValueToCheck==null || sValueToCheck.equals(""))continue; //empty ignore for (int j = 0; j < sValueTemp.length; j++) { if(i==j)continue; //same line ignore String sValueToCompare = sValueTemp[j]; if (sValueToCheck.equals(sValueToCompare)){ return true; } } } return false; } //check duplicated value private static boolean checkDuplicated_withSet(String[] sValueTemp) { Set sValueSet = new HashSet(); for(String tempValueSet : sValueTemp) { if (sValueSet.contains(tempValueSet)) return true; else if(!tempValueSet.equals("")) sValueSet.add(tempValueSet); } return false; } }