JAVA中怎么把字符串转换成字符数组

Rainly_boy 2010-07-02 12:11:11
RT。

今天遇到一题,要求 把字符串的每个字符按多到少的顺序输出,比如: Hello World 输出如下:
l(3)o(2)H(1)e(1)r(1)d(1).

看起来多简单的,可是把哥难住了 哎···


遇到问题总结如下:
(1) 字符串转换字符
(2) 多个循环
(3) 判断字符是否已经在前面出现过
(4) 等等
...全文
111309 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
haoyizsw 2010-07-04
  • 打赏
  • 举报
回复
学习了
shenglijining 2010-07-04
  • 打赏
  • 举报
回复
还好是Java,要是c的话就有点……
wuhanchendalong 2010-07-03
  • 打赏
  • 举报
回复
哎。。杯具!学习了
zyyoung 2010-07-03
  • 打赏
  • 举报
回复
string.getBytes()
网络咖啡 2010-07-03
  • 打赏
  • 举报
回复
自己写逻辑的代码啊
pp_psy 2010-07-03
  • 打赏
  • 举报
回复
设一个二维数组,num[][],num[][0]存字母,num[][1]存出现次数,后来做一个排序即可
用ascII码值来存储
songxing10000 2010-07-02
  • 打赏
  • 举报
回复
很强悍的代码我学习了
yktd26 2010-07-02
  • 打赏
  • 举报
回复
static Map sortMap(Map map) { 
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
int result = ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
return result == 0?
((Comparable) ((Map.Entry) (o1)).getKey())
.compareTo(((Map.Entry) (o2)).getKey())
:result;
}
});
Map result = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry)it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
lusongle 2010-07-02
  • 打赏
  • 举报
回复
dr_lou的代码可以,但是我觉得MAP是不排序的,还要按value来排序,这样怎么实现呢
重名率太高 2010-07-02
  • 打赏
  • 举报
回复
String的split()方法可以把一个字符串转换成一个字符数组(注意对空格的处理),然后再创建两个数组,一个保存字符串中的字母,一个保存字母出现的次数。最后对保存字母出现次数的数组进行排序,与保存字符串字母的数组关联输出。大概思路是这个样子,LZ可以尝试的编写一下代码
dr_lou 2010-07-02
  • 打赏
  • 举报
回复
package com.xuz.csdn.worldcup.day22;

import java.util.HashMap;
import java.util.Map;

public class HelloWorldCountTest {

public static void main(String[] args) {
String hello = "helloworld!";
Map<Character, Integer> map = new HashMap<Character, Integer>();
char[] ch = hello.toCharArray();
for (char c : ch) {
Integer i = map.get(c);
if (i == null) {
map.put(c, 1);
} else {
map.put(c, i.intValue() + 1);
}
}

System.out.println(map);
}

}
Apeipo 2010-07-02
  • 打赏
  • 举报
回复
1.字符串转字符
for(int i = 0; i < str.length ; i++ )
str.charAt(i);
2+3:不想循环的话 可以用一个List装字符,每次装之前调用if(List.contains(..))

13,100

社区成员

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

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