java数组去重复再排序思路

skyair2046 2009-10-27 01:40:47
就这么个样。没有思路阿
能告诉我大概要用到那些api还有思路
...全文
2200 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
jone33 2009-11-13
  • 打赏
  • 举报
回复
直接用CollectionUtils(setvalue,arrayvalue); OK
hardycheng 2009-10-27
  • 打赏
  • 举报
回复
先放入set,然后排序
即先去重复,后排序
zhuzeitou 2009-10-27
  • 打赏
  • 举报
回复
因为我实在写不出注释啊………………
skyair2046 2009-10-27
  • 打赏
  • 举报
回复
你的注释太少了。实在的很蒙啊
zhuzeitou 2009-10-27
  • 打赏
  • 举报
回复
额?有报错了?我测试很正常啊……
smallbear923 2009-10-27
  • 打赏
  • 举报
回复
System.out.println(Arrays.toString(dest));
会报错耶,说“类型 Object 中的方法 toString()对于参数(int[])不适用”。。。。
zhuzeitou 2009-10-27
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 skyair2046 的回复:]
引用 18 楼 zhuzeitou 的回复:
只用数组的话实现是没有问题的,但是很麻烦……因为数组无法改变大小的……

Java codeint[] src= {49,38,65,97,76,13,27,38,13 };int[] temp=newint[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=newint[count];
        System.arraycopy(temp,0, dest,0, count);
        System.out.println(Arrays.toString(dest));

尝试写注释……觉得写不出东西……

temp[count]= temp[j]^ temp[count];这是什么意思
[/Quote]

^表示异或,这是一种用来交换数组中两个数的方法,当然你也可以这么写
int n = temp[count];
temp[count] = temp[j];
temp[j] = n;
达到的效果是一样的
就是注意和本身交换的话需要作处理,要判断一下跳过
因为这个问题不会出现这种情况,所以就这样无所谓了
skyair2046 2009-10-27
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 zhuzeitou 的回复:]
只用数组的话实现是没有问题的,但是很麻烦……因为数组无法改变大小的……

Java codeint[] src= {49,38,65,97,76,13,27,38,13 };int[] temp=newint[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=newint[count];
System.arraycopy(temp,0, dest,0, count);
System.out.println(Arrays.toString(dest));

尝试写注释……觉得写不出东西……
[/Quote]
temp[count]= temp[j]^ temp[count];这是什么意思
jinancf 2009-10-27
  • 打赏
  • 举报
回复
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);
}
}
zhuzeitou 2009-10-27
  • 打赏
  • 举报
回复
上面是插入时直接去排序,也可以利用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));
chenzhp 2009-10-27
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 amdgaming 的回复:]
Java codepublicclass TestRepeat
{/**
*@param args*/publicstaticvoid 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]

[/Quote]

我认为很不错啊
zhuzeitou 2009-10-27
  • 打赏
  • 举报
回复
只用数组的话实现是没有问题的,但是很麻烦……因为数组无法改变大小的……

		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));


尝试写注释……觉得写不出东西……
jinancf 2009-10-27
  • 打赏
  • 举报
回复
先放到Set中,除去重复,同去取出排序
amdgaming 2009-10-27
  • 打赏
  • 举报
回复

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]
skyair2046 2009-10-27
  • 打赏
  • 举报
回复
还有就是能不能加点注释啊
skyair2046 2009-10-27
  • 打赏
  • 举报
回复
没有学这个类。才到数组,能不能就数组做出来哦。。
zhuzeitou 2009-10-27
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 viszl 的回复:]
int i[]=new int[]{1,2};
Set set=new TreeSet(Arrays.asList(i));
排序加去重都可以了
[/Quote]

似乎要写成
Integer i[] = new Integer[]{1, 2};
才行的吧?
zhuzeitou 2009-10-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 amdgaming 的回复:]
引用 8 楼 zhuzeitou 的回复:
额,直接扔进TreeSet,有必要的话自己写好Comparator,然后再转出来……

Java codepublicstaticvoid 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=newint[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
[/Quote]

额……效率问题再次被我忽略……某只大二时写出需要跑8秒的八皇后问题的猪泪奔………………
loveofmylife 2009-10-27
  • 打赏
  • 举报
回复
int i[]=new int[]{1,2};
Set set=new TreeSet(Arrays.asList(i));
排序加去重都可以了
amdgaming 2009-10-27
  • 打赏
  • 举报
回复
错字,每加, 一次。。。
加载更多回复(9)

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧