久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java從數組中刪除重復項?

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
本文介紹了Java從數組中刪除重復項?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我應該讀入一個包含許多不同電子郵件地址的文件并使用數組將它們打印出來.問題是我需要消除重復的電子郵件.

I am supposed to read in a file containing many different email addresses and print them out using an array. The problem is I need to eliminate duplicate emails.

我能夠讓我的 try/catch 工作并打印出電子郵件地址.但是,我不確定如何刪除重復項.我還不了解哈希碼或如何使用 Set.任何幫助將不勝感激.

I was able to get my try / catch working and print out the email addresses. However, I am not sure how to go about removing the duplicates. I do not have an understanding of hashcode's or how to use a Set yet. Any assistance would be appreciated.

這是我目前所擁有的:

import java.util.Scanner;
import java.io.*;

public class Duplicate {
   public static void main(String[] args) {

      Scanner keyboard = new Scanner(System.in);
      System.out.println("Enter file name: ");
      String fileName = keyboard.nextLine();
      if (fileName.equals("")) {
         System.out.println("Error: User did not specify a file name.");
      } else {
         Scanner inputStream = null;

         try {
            inputStream = new Scanner(new File(fileName));
         } catch (FileNotFoundException e) {
            System.out.println("Error: " + fileName + " does not exist.");
            System.exit(0);
         }

         String[] address = new String[100];

         int i = 0;
         while (inputStream.hasNextLine()) {
            String email = inputStream.nextLine();
            // System.out.println(email);

            address[i] = email;
            System.out.println(address[i]);
            i++;
         }
      }
   }
}

推薦答案

簡單的解決方案是使用java的Set,

The Simple solution is that use Set of java,

所以設置自動刪除重復值

so set remove duplicate value automatically

在您的代碼中,您有數組而不是轉換數組以直接使用代碼設置

and in your code you have array than convert array to set directly using code

Set<T> mySet = new HashSet<T>(Arrays.asList(someArray));

這篇關于Java從數組中刪除重復項?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
Using BouncyCastle to encrypt with ECIES in Java(在 Java 中使用 BouncyCastle 使用 ECIES 進行加密)
主站蜘蛛池模板: aaaa一级毛片| 一级片免费视频 | 国产精品国产a | 成人免费大片黄在线播放 | 国产一区二区三区精品久久久 | 国产在线观看网站 | 五月槐花香 | 精品免费国产视频 | 国产xxx在线观看 | 中文字幕免费视频 | 草比网站 | 亚洲午夜精品一区二区三区他趣 | h片在线看 | 成人欧美一区二区三区在线观看 | 91欧美激情一区二区三区成人 | 日本精品一区二区三区视频 | 九九久久久 | 极品电影院 | 国产成人综合在线 | 中文字幕亚洲在线 | 色综合色综合色综合 | 日韩中文字幕av | 午夜在线免费观看视频 | 精品日韩 | 日韩成人一区 | 久久1区 | 97精品国产一区二区三区 | 久久高清免费视频 | 男女羞羞视频免费 | 国产一区二区精品在线 | 91精品国产91综合久久蜜臀 | 国产成人在线播放 | 99reav| 国产精品一区二区三区在线 | 在线看日韩av | 国产极品车模吞精高潮呻吟 | 一起操网站 | 岛国av免费在线观看 | 中文字幕一区二区三区四区五区 | 国产一区 | 亚洲免费高清 |