List list = new ArrayList(); List list1 = new ArrayList(); list.add("a"); list.add("b"); list.add("b"); list.add("e"); list.add("i"); list.add("v"); list.add("p"); list.add("a"); list.add("b"); list.add("b"); list.add("e"); list.add("i"); list.add("v"); list.add("p"); Set set = new HashSet();
set.addAll(list);
for(java.util.Iterator it = set.iterator(); it.hasNext(); ) { list1.add(it.next()); } java.util.Collections.sort(list1); System.out.println(list1);运行结果:[a, b, e, i, p, v]
额,直接扔进TreeSet,有必要的话自己写好Comparator,然后再转出来……public static void main(String[] args) { int[] src = { 49, 38, 65, 97, 76, 13, 27, 38, 13 }; Set<Integer> set = new TreeSet<Integer>(); for (int i : src) { set.add(i); } int[] dest = new int[set.size()]; Iterator<Integer> iter = set.iterator(); for (int i = 0; i < dest.length && iter.hasNext(); i++) { dest[i] = iter.next(); } System.out.println(Arrays.toString(dest)); }输出结果: [13, 27, 38, 49, 65, 76, 97]
to: 楼上,treeset效率非常低,没加一个元素 要排序依次,,,如果数据多的话 发狂 ing
int i[]=new int[]{1,2}; Set set=new TreeSet(Arrays.asList(i)); 排序加去重都可以了
List list = new ArrayList();//new一个arralist Set set = new HashSet();//new 一个hashset set.addAll(java.util.Arrays.asList(str));//将数组转为list并存入set中,就可以去掉重复项了 for (java.util.Iterator it = set.iterator(); it.hasNext();) { list.add(it.next());//遍历set 将所有元素键入list中 } java.util.Collections.sort(list); //对list进行快速排序 System.out.println(list);//打印结果 }结果:[a, am, bs, ce, e, ee, gg, jj, love, mm, rr, sb, tt, you, yy]
先放到Set中,除去重复,同去取出排序
只用数组的话实现是没有问题的,但是很麻烦……因为数组无法改变大小的…… int[] src = { 49, 38, 65, 97, 76, 13, 27, 38, 13 }; int[] temp = new int[src.length]; int count = 0; for (int i = 0; i < src.length; i++) { temp[count] = src[i]; for (int j = 0; j < count; j++) { if (temp[j] == temp[count]) { count--; break; } if (temp[j] > temp[count]) { temp[count] = temp[j] ^ temp[count]; temp[j] = temp[j] ^ temp[count]; temp[count] = temp[j] ^ temp[count]; } } count++; } int[] dest = new int[count]; System.arraycopy(temp, 0, dest, 0, count); System.out.println(Arrays.toString(dest));尝试写注释……觉得写不出东西……
上面是插入时直接去排序,也可以利用Arrays.sort排序 int[] src = { 49, 76, 38, 13, 27, 38, 65, 97, 13 }; int[] temp = new int[src.length]; int count = 0; for (int i = 0; i < src.length; i++) { temp[count] = src[i]; for (int j = 0; j < count; j++) { if (temp[j] == temp[count]) { count--; break; } } count++; } int[] dest = new int[count]; System.arraycopy(temp, 0, dest, 0, count); Arrays.sort(dest); System.out.println(Arrays.toString(dest));
import java.util.*;public class TestRepeat { public static void main(String[] args) { String[] str = { "a", "e", "ee", "rr", "ce", "love", "you", "am", "sb", "bs", "jj", "gg", "mm", "love", "a", "e", "tt", "yy" }; List list = new ArrayList(); Set set = new TreeSet(); set.addAll(java.util.Arrays.asList(str)); System.out.println(set); } }
temp[count]= temp[j]^ temp[count];这是什么意思
^表示异或,这是一种用来交换数组中两个数的方法,当然你也可以这么写 int n = temp[count]; temp[count] = temp[j]; temp[j] = n; 达到的效果是一样的 就是注意和本身交换的话需要作处理,要判断一下跳过 因为这个问题不会出现这种情况,所以就这样无所谓了
将你数组元素逐个放入 hashset中,如何在根据Collections。sort()实现自己的排序规则的比较接口
就可以了。。
List list = new ArrayList();
List list1 = new ArrayList();
list.add("a");
list.add("b");
list.add("b");
list.add("e");
list.add("i");
list.add("v");
list.add("p");
list.add("a");
list.add("b");
list.add("b");
list.add("e");
list.add("i");
list.add("v");
list.add("p");
Set set = new HashSet();
set.addAll(list);
for(java.util.Iterator it = set.iterator(); it.hasNext(); )
{
list1.add(it.next());
}
java.util.Collections.sort(list1);
System.out.println(list1);运行结果:[a, b, e, i, p, v]
int[] src = { 49, 38, 65, 97, 76, 13, 27, 38, 13 };
Set<Integer> set = new TreeSet<Integer>();
for (int i : src) {
set.add(i);
}
int[] dest = new int[set.size()];
Iterator<Integer> iter = set.iterator();
for (int i = 0; i < dest.length && iter.hasNext(); i++) {
dest[i] = iter.next();
}
System.out.println(Arrays.toString(dest));
}输出结果:
[13, 27, 38, 49, 65, 76, 97]
发狂 ing
Set set=new TreeSet(Arrays.asList(i));
排序加去重都可以了
似乎要写成
Integer i[] = new Integer[]{1, 2};
才行的吧?
public class TestRepeat
{ /**
* @param args
*/
public static void main(String[] args)
{ String[] str = { "a", "e", "ee", "rr", "ce", "love", "you", "am", "sb", "bs", "jj", "gg", "mm", "love", "a",
"e", "tt", "yy" };//定义一个数组
List list = new ArrayList();//new一个arralist
Set set = new HashSet();//new 一个hashset
set.addAll(java.util.Arrays.asList(str));//将数组转为list并存入set中,就可以去掉重复项了
for (java.util.Iterator it = set.iterator(); it.hasNext();)
{
list.add(it.next());//遍历set 将所有元素键入list中
}
java.util.Collections.sort(list); //对list进行快速排序
System.out.println(list);//打印结果 }结果:[a, am, bs, ce, e, ee, gg, jj, love, mm, rr, sb, tt, you, yy]
int[] temp = new int[src.length];
int count = 0;
for (int i = 0; i < src.length; i++) {
temp[count] = src[i];
for (int j = 0; j < count; j++) {
if (temp[j] == temp[count]) {
count--;
break;
}
if (temp[j] > temp[count]) {
temp[count] = temp[j] ^ temp[count];
temp[j] = temp[j] ^ temp[count];
temp[count] = temp[j] ^ temp[count];
}
}
count++;
}
int[] dest = new int[count];
System.arraycopy(temp, 0, dest, 0, count);
System.out.println(Arrays.toString(dest));尝试写注释……觉得写不出东西……
int[] temp = new int[src.length];
int count = 0;
for (int i = 0; i < src.length; i++) {
temp[count] = src[i];
for (int j = 0; j < count; j++) {
if (temp[j] == temp[count]) {
count--;
break;
}
}
count++;
}
int[] dest = new int[count];
System.arraycopy(temp, 0, dest, 0, count);
Arrays.sort(dest);
System.out.println(Arrays.toString(dest));
public static void main(String[] args) {
String[] str = { "a", "e", "ee", "rr", "ce", "love", "you", "am", "sb",
"bs", "jj", "gg", "mm", "love", "a", "e", "tt", "yy" }; List list = new ArrayList();
Set set = new TreeSet();
set.addAll(java.util.Arrays.asList(str));
System.out.println(set);
}
}
^表示异或,这是一种用来交换数组中两个数的方法,当然你也可以这么写
int n = temp[count];
temp[count] = temp[j];
temp[j] = n;
达到的效果是一样的
就是注意和本身交换的话需要作处理,要判断一下跳过
因为这个问题不会出现这种情况,所以就这样无所谓了
会报错耶,说“类型 Object 中的方法 toString()对于参数(int[])不适用”。
即先去重复,后排序