本文目錄一覽:
JAVA關於順序數組數據去重,效率最高的方式是什麼?
JAVA關於順序數組數據去重,效率最高的方式是使用LinkedHashSet也是Set,set的特徵就是對重複的元素只保存一個,LinkedHashSet只是在內部使用鏈表維護元素插入的順序
package com.question;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.LinkedHashSet;
/**
* delete the conflict String.
*
* @author Xxx
*/
public class Q16 {
/**
* generate the text.
*
*/
public void init() {
// write file
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(“C:/init.txt”);
for (int i = 0; i 100000; i++) {
for (int j = 0; j 2; j++) {
outputStream.write((“Hello” + i).getBytes());
outputStream.write(“\r\n”.getBytes());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
outputStream = null;
}
}
}
/**
* filter the string.
*
* @return
*/
public LinkedHashSetString filter() {
// create a LinkedHashSet project.
LinkedHashSetString linkedHashSet = new LinkedHashSetString();
try {
// read the file.
InputStream inputStream = new FileInputStream(“C:/init.txt”);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = bufferedReader.readLine();
// add the string to the LinkedHashSet
while(line != null) {
linkedHashSet.add(line);
line = bufferedReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return the result.
return linkedHashSet;
}
@Deprecated
public static void main(String[] args) {
Q16 q16 = new Q16();
// q16.init();
LinkedHashSetString linkedHashSet = q16.filter();
System.out.println(linkedHashSet.size());
}
}
java中怎麼找出數組中重複的數並去除?
java中找出數組中重複的數並去除的代碼如下:
public static void testA() {
String [] str = {“Java”, “C++”, “Php”, “C#”, “Python”, “C++”, “Java”};
for (String elementA:str ) {
System.out.print(elementA + ” “);
}
ListString list = new ArrayListString();
for (int i=0; istr.length; i++) {
if(!list.contains(str[i])) {
list.add(str[i]);
}
}
System.out.println();
String[] newStr = list.toArray(new String[1]); //返回一個包含所有對象的指定類型的數組
for (String elementB:newStr ) {
System.out.print(elementB + ” “);
}
System.out.println();
}
所謂數組,是無序的元素序列。 若將有限個類型相同的變量的集合命名,那麼這個名稱為數組名。組成數組的各個變量稱為數組的分量,也稱為數組的元素,有時也稱為下標變量。用於區分數組的各個元素的數字編號稱為下標。數組是在程序設計中,為了處理方便, 把具有相同類型的若干元素按無序的形式組織起來的一種形式。 這些無序排列的同類數據元素的集合稱為數組。
舉例:
int a[10]; 說明整型數組a,有10個元素。
float b[10],c[20]; 說明實型數組b,有10個元素,實型數組c,有20個元素。
char ch[20]; 說明字符數組ch,有20個元素。
數組中的所有元素都具有相同類型(這一點和結構或類中的字段不同,它們可以是不同類型)。數組中的元素存儲在一個連續性的內存塊中,並通過索引來訪問(這一點也和結構和類中的字段不同,它們通過名稱來訪問)。
java中怎麼樣子找出數組中重複的數,並去除
;
public static void main(String[] args) {
//可以換種思路,把數組放到set裡面(set的值不會重複)就可以去重了
Integer[] arr = {85,4,2,6,11,4,5,8,9};
SetInteger set = new HashSetInteger();
for(Integer i : arr)
set.add(i);
for(Object j: set.toArray())
System.out.print(j + ” “);
}
java中數組怎麼刪除數組中重複的數
通過HashSet剔除
// 刪除ArrayList中重複元素,add進去順序就變了不考慮順序的話可以使用
public static void removeDuplicate1(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
System.out.println(list);
}
原創文章,作者:KFAC,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/132974.html