佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1502|回复: 7

Java - Sort & Remove Duplicate in Array

[复制链接]
发表于 1-12-2009 01:20 AM | 显示全部楼层 |阅读模式
如题。我要的结果是012,要如何做?谢谢!

String r = "1021";

char[] content = r.toCharArray();
Arrays.sort(content);
String result = new String(content);

out.print("Result:" + result);
回复

使用道具 举报


ADVERTISEMENT

发表于 1-12-2009 09:05 AM | 显示全部楼层
String r = "1021";
                String sortString = "";

                HashMap HostCount = new HashMap();

                for (int i = 0; i < r.length(); i++) {
                        HostCount.put(r.charAt(i), r.charAt(i));
                }

                Set HostKeys = HostCount.keySet();
                Iterator It = HostKeys.iterator();
                while (It.hasNext()) {
                        Character HostNow =  (Character)It.next();
                        sortString += "" + HostCount.get(HostNow);
                }
               
                System.out.println ("SortString : " + sortString);

不过 我 RETUNR 回 210 。。。 LZ 自己 SORT 。。
回复

使用道具 举报

 楼主| 发表于 1-12-2009 12:17 PM | 显示全部楼层
谢谢你。你真棒
回复

使用道具 举报

 楼主| 发表于 1-12-2009 01:15 PM | 显示全部楼层
对不起。为什么有Warning的?
warning:  [unchecked] unchecked call to put (K, V) as a member of the raw type java.util.HashMap

我把 HashMap HostCount = new HashMap(); 改成HashMap HostCount<String, String> = new HashMap<String, String>(); 都不能。请帮忙,谢了。
回复

使用道具 举报

发表于 1-12-2009 01:23 PM | 显示全部楼层
  1. HostCount.put(r.charAt(i), r.charAt(i));
复制代码

注意type
回复

使用道具 举报

发表于 1-12-2009 03:41 PM | 显示全部楼层
原帖由 mikeng 于 1-12-2009 01:15 PM 发表
对不起。为什么有Warning的?
warning:   unchecked call to put (K, V) as a member of the raw type java.util.HashMap

我把 HashMap HostCount = new HashMap(); 改成HashMap HostCount = new HashMap(); 都不 ...


不用 DECLARE PARAMETER 的 。
回复

使用道具 举报

Follow Us
 楼主| 发表于 1-12-2009 04:04 PM | 显示全部楼层
嘻!谢谢您
回复

使用道具 举报

发表于 25-4-2010 11:09 PM | 显示全部楼层
你可以用TreeSet
回复

使用道具 举报


ADVERTISEMENT

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 24-11-2025 03:18 PM , Processed in 1.241976 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表