Java如何将数组中具有相同的元素都删去 Java数组中重复数据的删除
java\u4e2d\u600e\u4e48\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u7684\u5143\u7d20\u53bb\u6389\uff0c\u4f46\u662f\u8fd8\u662f\u4fdd\u7559\u4e00\u4e2a\u3002import java.util.*;
class BaiDu
{
public static void main(String[] args)
{
TreeSet tr = new TreeSet();
String[] s ={"11","22","22","33","33","33"};
System.out.print("====\u5904\u7406\u524d=======");
for(int i=0;i<s.length;i++){
System.out.print(s[i]+" ");
tr.add(s[i]);
}
String[] s2= new String[tr.size()];
System.out.println("=====\u5904\u7406\u540e======");
for(int i=0;i<s2.length;i++){
s2[i]=tr.pollFirst();//\u4eceTreeSet\u4e2d\u53d6\u51fa\u5143\u7d20\u91cd\u65b0\u8d4b\u7ed9\u6570\u7ec4
System.out.print(s2[i]+" ");
}
}
}
java\u6570\u7ec4\u4e2d\u53bb\u6389\u91cd\u590d\u6570\u636e\u53ef\u4ee5\u4f7f\u7528set\u96c6\u5408\uff0cset\u672c\u8eab\u5185\u90e8\u662f\u4e0d\u4fdd\u5b58\u91cd\u590d\u7684\u6570\u636e\u7684\uff0c\u5982\u4e0b\u4ee3\u7801\uff1a
import java.util.Arrays;import java.util.Set;import java.util.TreeSet;public class ceshi {public static void main(String[] args) {int[] testArr = { 5, 5, 1, 2, 3, 6, -7, 8, 6, 45, 5 };//\u65b0\u5efa\u4e00\u4e2aint\u7c7b\u578b\u6570\u7ec4System.out.println(Arrays.toString(testArr));Set set = new TreeSet();//\u65b0\u5efa\u4e00\u4e2aset\u96c6\u5408for (int i : testArr) {set.add(i);}Integer[] arr2 = set.toArray(new Integer[0]);// \u6570\u7ec4\u7684\u5305\u88c5\u7c7b\u578b\u4e0d\u80fd\u8f6c \u53ea\u80fd\u81ea\u5df1\u8f6c\uff1b\u5427Integer\u8f6c\u4e3a\u4e3aint\u6570\u7ec4\uff1bint[] result = new int[arr2.length];for (int i = 0; i < result.length; i++) {result[i] = arr2[i];}System.out.println(Arrays.toString(arr2));}}\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a
比如现在有个数组为 myArray ,里面有部分的重复元素
Set mySet = new HashSet();
for(Object obj : Array){
mySet.add(obj);
}
mySet中所保存的元素就是唯一的了.
再吧mySet保存到数组中
完整例子:
// 创建一个数组,里面存在重复的元素
String[] myArray = {"s","s","f","d"};
Set<String> mySet = new HashSet<String>();
// 去除重复元素
for(String s : myArray){
mySet.add(s);
}
myArray = new String[mySet.size()];
int index = 0;
// 将去重后的结果存入数组
for(String s : mySet){
myArray[index] = s;
index++;
}
// 打印出来结果
System.out.println(Arrays.toString(myArray));
ArrayList a = new ArrayList(数组);
for(int i = 0 ;i<a.size();i++){
Object obj = 数组[i];
if(a.contains(obj)){
a.remove(i);
}
}
我答案错了,不好意思,当初咋想的忘记了,抱歉。下面的其他匿名网友回答是正确的。另外附上我的修改。
一:利用set的唯一性解决
String[] strArr = {"B","A","C","D","C","A"};
Set<String> strList = new HashSet(Arrays.asList(strArr));
System.out.println(strList.toString());
二:创建新的存储单位,判断是否重复后逐个增加
public static void main(String[] args){
String[] strArr = {"B","A","C","D","C","A"};
List<String> strList = new ArrayList<String>();
for (String str : strArr){
if(strList.contains(str)){
continue;
}
strList.add(str);
}
System.out.println(strList.toString());
}
三:利用steam的distinct方法去重(jdk1.8的新特性)
String[] strArr = {"B","A","C","D","C","A"};
List<String> strList = Arrays.asList(strArr);
strList = strList.stream().distinct().collect(Collectors.toList());
System.out.println(strList.toString());
用HashSet,将所有的元素放入到hashset里面,再读出来相同的元素就都删除只剩下一个了。
将数组放入set中后在转化到数组就行啦。
想知道,你最后是如何解决的
绛旓細瀹氫箟鏁扮粍a[],b[]for(int i = 0; i<a.length; i++) { for(int j = 0; j< b.length; j++){ if(a[i] == b[j]){ 鏈夌浉鍚鍏冪礌 } } } 鐢ㄦ暟缁刟涓殑姣忎竴涓嬁鍑烘潵涓庢暟缁刡涓殑姣忎竴涓幓鍖归厤锛屽尮閰嶄笂浜嗗氨鎴愮珛
绛旓細鍙互鐨勩傝姣旇緝涓や釜鏁扮粍鐨勬墍鏈夊兼槸鍚︾浉绛夛紝瑕佷娇鐢╢or锛堬級寰幆鏉ユ瘮杈冿紝灏辨槸涓や釜鏁扮粍鐨勯暱搴︽槸鐩稿悓鐨锛宖or寰幆濂藉啓锛屽灞傜殑涓庡唴灞傜殑闀垮害涓鑷淬俧or(...;i<aa.length;i++){ for(){ } }銆傚娉細濡傛灉涓や釜鏁扮粍鐨勯暱搴︿笉涓鑷达紝閭e氨娌℃硶姣旇緝浜嗭紙鍓嶆彁鏄袱涓暟缁勯兘鏄弧鐨勶級...
绛旓細find;for(int i=0;i<oneArray.length;i++){ find=false;for(int j=0;j<twoArray.length;j++){ if(oneArray[i]==twoArray[j]){ if(!find){ count++;find=true;} twoArray[j]=-1;} } } System.out.println("鏁扮粍oneArray涓庢暟缁則woArray鍏鏈"+count+"涓鐩稿悓鐨鍏冪礌锛");} } ...
绛旓細public class ArrayUnique { / 鏂规硶1 灏辨槸鐢宠涓涓拰nums澶у皬涓鏍风殑鏁扮粍tmpNums锛岀劧鍚庨亶鍘唍ums涓殑鍏冪礌锛屽姣忎釜鍏冪礌鍒ゆ柇鏄惁鍦╰mpNums鍑虹幇杩囷紝濡傛灉鍑虹幇杩囷紝閭d箞灏变笉鏀惧埌鏂鏁扮粍閲岄潰锛屼篃灏辨槸涓嶅仛澶勭悊锛涘鏋滄病鏈夊嚭鐜拌繃锛岄偅涔堝氨鎶瀹冩斁鍒版柊鐨勬暟缁勯噷闈 杩欑鏂规硶鏁扮粍鍘熸潵鐨勭浉瀵归『搴忓彲浠ヤ繚璇 鏃堕棿澶嶆潅搴︽槸n^2鍜...
绛旓細import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class Test022 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub List<Item> list = new ArrayList<Item>(); list.add(new I...
绛旓細鏈nlogn鐨,鍏堝涓や釜鏁扮粍杩涜鎺掑簭,鐒跺悗鍐嶆嬁鍏朵腑鐨涓涓暟瀛楀幓鍙︿竴涓暟瀛椾腑浜屽垎鏌ユ壘.
绛旓細鏁扮粍涓鎻愬彇鐩稿悓鐨瀛楁瘝锛屽彲浠ヤ娇鐢╯tring鐨刢harat鏂规硶閬嶅巻瀛楁瘝锛屾斁鍒颁竴涓猻tring涓紝鎵惧埌涔嬪悗鍒犻櫎鎺夎繖涓瓧姣嶏紝鎺ョ潃閬嶅巻涓嬩竴涓紝绀轰緥濡備笅锛歱ackage test1;import java.util.Random;public class ArrayTest {public static void main(String[] args) {//1銆佹瀯寤轰竴涓暟缁凷tring[] array={"a","b","c","d"...
绛旓細鍏跺疄寰堢畝鍗曪紝瀹屽叏涓嶇敤鐢ㄩ泦鍚堬紝鍙涓鎵惧ソ灏眕rint鎴栫粍String锛屽棣鐨勫唴瀛樹篃鍙互鐪佷笅 public class Run { private final static int [] temp = {1,2,5,8,6,2,11,15,2,6,2,11,15,5,1,8,6};public static void main(String[] args) { Run r = new Run();r.locateAllAndPrint();}...
绛旓細int[] buffered = new int[10]; // 澹版槑涓涓复鏃舵暟瀛楃被鍨嬬殑鏁扮粍 // 瑕佹悶娓呮锛岃繖涓暟缁勫叾瀹炴湁 11 涓綅缃紝鎴戠幇鍦ㄥ彧鐢ㄤ簡鍏朵腑10涓!// 寰幆缁欒繖涓暟缁勮祴鍊 for (int i = 0; i < buffered.length; i++) { buffered[i] = random.nextInt(100);} System.out.println("--- 鐢熸垚缁撴灉 --...
绛旓細Scanner scan = new Scanner(System.in);Random random = new Random((long) Math.random());List<int[]> list = new ArrayList();System.out.println("杈撳叆 Stop 缁撴潫杈撳叆");while (true) { System.out.println("宸叉湁鏁扮粍鏁伴噺锛" + list.size()); System.out.println("杈撳叆鏁扮粍闀...