ArrayList清除重复 谢谢。
1 2 11 3 1
1 4 1
1 5 1
1 6 1
2 1 1
2 3 1
2 4 1
2 7 1
3 1 1
3 2 1
4 1 1
4 2 1
4 5 1
5 1 1
5 4 1
5 6 1
6 1 1
6 5 1
6 7 1
7 2 1
7 6 1
以上每一行作为一个String[]数组存在ArrayList里面,目的是把1 2 1 和 2 1 1这些前2个字符串相反的数组元素删除其中一个,谢谢。 --------------------编程问答-------------------- 使用Set --------------------编程问答-------------------- 一个一个遍历就好了,很简单的。话说你这个数据量有多大?有没有时间要求? --------------------编程问答-------------------- 貌似ArrayList不能把一个String[]作为一个元素进行添加吧? --------------------编程问答-------------------- 用Map的key存储前2位数字和,存储前先判断key是否存在,不存在则加入 --------------------编程问答-------------------- 一种就是放到 set ,set会把重复的去掉.
一种就是循环,发现重复的去掉.
--------------------编程问答-------------------- 其实我也很想知道 如果不用SET 和Map的机制 单从List本身 或则 怎么能快速的找出重复的数据呢? --------------------编程问答--------------------
List<String[]> list = new ArrayList<String[]>();
//假设list里面是
/*
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
2 1 1
2 3 1
2 4 1
2 7 1
*/
HashMap<String, String> map = new HashMap<String, String>();
for(int i =0;i<list.size();i++){
String temp = map.get(list.get(i)[0]);
if(temp!=null&&temp.equals(list.get(i)[1])){
list.remove(i);
}
else {
map.put(list.get(i)[0], list.get(i)[1]);
}
}
//OK了~~~~~~~~~~~~~
试试这样。。。。。。。。 --------------------编程问答--------------------
String temp = map.get(list.get(i)[1]);
if(temp!=null&&temp.equals(list.get(i)[0])){
list.remove(i);
}
else {
map.put(list.get(i)[0], list.get(i)[1]);
}
上面代码取反值了,应该按照这样取值比较。。。楼主试试 --------------------编程问答--------------------
就是对List运用Set运用了的去重算法,貌似相当于冒泡排序,时间复杂度O(n^2) --------------------编程问答--------------------
public static void main(String[] args) {
List<String[]> list = new ArrayList<String[]>();
String[] strs1 = {"1","2","1"};
String[] strs2 = {"1","3","1"};
String[] strs3 = {"1","4","1"};
String[] strs4 = {"1","5","1"};
String[] strs5 = {"2","1","1"};
String[] strs6 = {"3","1","1"};
String[] strs7 = {"2","3","1"};
String[] strs8 = {"3","2","1"};
list.add(strs1);
list.add(strs2);
list.add(strs3);
list.add(strs4);
list.add(strs5);
list.add(strs6);
list.add(strs7);
list.add(strs8);
HashMap<String, String> map = new HashMap<String, String>();
for(int i =0;i<list.size();i++){
String temp = map.get(list.get(i)[0]);
if(temp!=null&&temp.equals(list.get(i)[1])){
list.remove(i);
i--;
}
else {
map.put(list.get(i)[1], list.get(i)[0]);
}
}
//OK了~~~~~~~~~~~~~
for(String[] strs:list){
for(String str:strs){
System.out.print(str+" ");
}
System.out.println();
}
}
好了,测试没有问题。。。。。
输出结果:
1 2 1
1 3 1
1 4 1
1 5 1
2 3 1 --------------------编程问答-------------------- 可以多list 进行排序 重写排序算法,按照1 2 1 和 2 1 1这些前2个字符串相反的数组元素 这个规律把类似的放在一起,或者看直接能删除不。 --------------------编程问答-------------------- 这个应该是可以的。
--------------------编程问答-------------------- 仅仅是排序可以放在一起,并不能解决楼主问题。
--------------------编程问答--------------------
public class Element {
private String first;
private String second;
public Element(String[] str){
first = str[0];
second = str[1];
}
public String getFirst(){
return first;
}
public String getSecond(){
return second;
}
@Override
public boolean equals(Object obj) {
Element e = (Element) obj;
if (first.equals(e.getSecond()) && second.equals(e.getFirst())) {
return true;
}
return false;
}
@Override
public int hashCode() {
return first.hashCode() ^ second.hashCode();
}
}
HashSet<Element> set = new HashSet<Element>();
String[] strs1 = {"1","2","1"};
String[] strs2 = {"1","3","1"};
String[] strs3 = {"1","4","1"};
String[] strs4 = {"1","5","1"};
String[] strs5 = {"2","1","1"};
String[] strs6 = {"3","1","1"};
String[] strs7 = {"2","3","1"};
String[] strs8 = {"3","2","1"};
set.add(new Element(strs1));
set.add(new Element(strs2));
set.add(new Element(strs3));
set.add(new Element(strs4));
set.add(new Element(strs5));
set.add(new Element(strs6));
set.add(new Element(strs7));
set.add(new Element(strs8));
List<Element> list = new ArrayList<Element>();
list.addAll(set);
面向对象的做法 --------------------编程问答-------------------- 10, 14楼都可以,不过14楼不是纯String[]对象元素了, 还有
if (first.equals(e.getSecond()) && second.equals(e.getFirst())) {
return true;
}
equals本身有具有对称性,对称再比较多余了吧 --------------------编程问答-------------------- 直接放set里面,set集合是不允许有重复的,有重复的值会直接跳过,然后再继续加 --------------------编程问答-------------------- --------------------编程问答-------------------- 嗯 不错 学习了 --------------------编程问答-------------------- 如果不用SET 和Map的机制 单从List本身
我这种方式是可以的,放在一起,一个for循环就把多余的删除
--------------------编程问答--------------------
package practice;
/*
*以楼猪前15字符串数组为例
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
2 1 1
2 3 1
2 4 1
2 7 1
3 1 1
3 2 1
4 1 1
4 2 1
4 5 1
5 1 1
*/
import java.util.ArrayList;
import java.util.List;
public class Quchong {
public static void main(String[] args) {
List<String[]> list = new ArrayList<String[]>();
String[] strs1 = { "1", "2", "1" };
String[] strs2 = { "1", "3", "1" };
String[] strs3 = { "1", "4", "1" };
String[] strs4 = { "1", "5", "1" };
String[] strs5 = { "1", "6", "1" };
String[] strs6 = { "2", "1", "1" };
String[] strs7 = { "2", "3", "1" };
String[] strs8 = { "2", "4", "1" };
String[] strs9 = { "2", "7", "1" };
String[] strs10 = { "3", "1", "1" };
String[] strs11 = { "3", "2", "1" };
String[] strs12 = { "4", "1", "1" };
String[] strs13 = { "4", "2", "1" };
String[] strs14 = { "4", "5", "1" };
String[] strs15 = { "5", "1", "1" };
list.add(strs1);
list.add(strs2);
list.add(strs3);
list.add(strs4);
list.add(strs5);
list.add(strs6);
list.add(strs7);
list.add(strs8);
list.add(strs9);
list.add(strs10);
list.add(strs11);
list.add(strs12);
list.add(strs13);
list.add(strs14);
list.add(strs15);
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); j++) {
String str1 = list.get(i)[0];
String str2 = list.get(i)[1];
String str3 = list.get(j)[1];
String str4 = list.get(j)[0];
if (str1.equals(str3) && str2.equals(str4)) {
list.remove(i);
}
}
}
for (String[] strs : list) {
for (String str : strs) {
System.out.print(str + " ");
}
System.out.println();
}
}
}
我直接用ArrayList做的,用了两个。测了没问题的,输出结果:
1 6 1
2 1 1
2 7 1
3 1 1
3 2 1
4 1 1
4 2 1
4 5 1
5 1 1 --------------------编程问答-------------------- 先对每一行进行字符串拆分排序,如121和211
直接就排成了112 112.然后放入set
String s1= "1 2 1";
String s2= "2 1 1";
Arrays.sort(s1.split(" "));
Arrays.sort(s2.split(" "));
Set s = new HashSet();
s.add(s1);
s.add(s2);
//如果还要将set转成list
List list = new ArrayList(s);
手写的,难免出错,小错你自已改下 --------------------编程问答-------------------- 希望这篇博文对你有帮助,也是讲解List去掉重复元素
http://blog.csdn.net/yuexiaqingsi/article/details/7971876 --------------------编程问答-------------------- 用set啊。。你去试下
补充:Java , Java SE