久久久久久久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 進行加密)
主站蜘蛛池模板: 22精品一区二区三区 | 成人网av| 日韩视频免费在线观看 | www国产| 欧美专区第一页 | 综合导航| 色网站在线观看 | 狠狠干美女 | 91精品国产综合久久久蜜臀九色 | 国产精品偷乱一区二区三区 | 一级毛片播放 | 又大又黄又爽 | 99久久综合| 国产毛片毛片毛片 | 久久久久久黄色 | 国产精品一区在线观看 | 伊人久久久久久久久久 | 亚洲精品91天天久久人人 | 亚洲色网址 | 一级片免费在线观看 | 色综合99| 中文字幕一二三四区 | 夜色在线影院 | av中文在线 | 天海翼在线视频 | 亚洲精品网址 | av在线资源网 | 成人免费视频网址 | 精品欧美一区二区精品久久 | 欧美视频区| 日日干视频 | 久久国产综合 | 亚洲久久在线 | 一级做a视频 | 中文字幕激情 | 二区三区在线观看 | 国产区一区 | 黄色91网站| 黄色免费观看网站 | 国产精品日日摸天天碰 | 在线免费黄色网址 |