java如何统计字符串中每个字符出现的次数
比如我输入abbcccddd系统则输出a(1)b(2)c(3)d(3)忘记告诉大家了,我的参数是从命令行的到的,通过main函数的args参数得到比如:C:\>javaT...
比如我输入 abbcccddd
系统则输出 a(1)b(2)c(3)d(3)
忘记告诉大家了,我的参数是从命令行的到的,通过main函数的args参数得到 比如: C:\> java Test abbcccdddd 回车
而且要排序输出 展开
系统则输出 a(1)b(2)c(3)d(3)
忘记告诉大家了,我的参数是从命令行的到的,通过main函数的args参数得到 比如: C:\> java Test abbcccdddd 回车
而且要排序输出 展开
2个回答
展开全部
正确答案:
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class Test {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
String str = null;
try {
str = args[0];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("请输入参数!");
System.exit(0);
}
Map tree = new TreeMap();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
if (!tree.containsKey(ch)) {
tree.put(ch, new Integer(1));
} else {
Integer in = (Integer) tree.get(ch) + 1;
tree.put(ch, in);
}
}
}
Iterator tit = tree.keySet().iterator();
while (tit.hasNext()) {
Object temp = tit.next();
System.out.print(temp.toString() + "(" + tree.get(temp) + ")");
}
}
}
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class Test {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
String str = null;
try {
str = args[0];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("请输入参数!");
System.exit(0);
}
Map tree = new TreeMap();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
if (!tree.containsKey(ch)) {
tree.put(ch, new Integer(1));
} else {
Integer in = (Integer) tree.get(ch) + 1;
tree.put(ch, in);
}
}
}
Iterator tit = tree.keySet().iterator();
while (tit.hasNext()) {
Object temp = tit.next();
System.out.print(temp.toString() + "(" + tree.get(temp) + ")");
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package org.zj.sample;
public class Test {
char array_char[];
public int getInstances(String all, String choice) {
int total=0;
array_char=all.toCharArray();
for(int i=0;i<array_char.length;i++){
if (array_char[i]==(choice.charAt(0))){
total++;
}
}
return total;
}
public static void main(String[] args){
String appearance=new String("ADBCA ADCBACDBC BDA");
Test t=new Test();
System.out.println("A: " + t.getInstances(appearance, "A"));
System.out.println("B: " + t.getInstances(appearance, "B"));
System.out.println("C: " + t.getInstances(appearance, "C"));
System.out.println("D: " + t.getInstances(appearance, "D"));
}
}
public class Test {
char array_char[];
public int getInstances(String all, String choice) {
int total=0;
array_char=all.toCharArray();
for(int i=0;i<array_char.length;i++){
if (array_char[i]==(choice.charAt(0))){
total++;
}
}
return total;
}
public static void main(String[] args){
String appearance=new String("ADBCA ADCBACDBC BDA");
Test t=new Test();
System.out.println("A: " + t.getInstances(appearance, "A"));
System.out.println("B: " + t.getInstances(appearance, "B"));
System.out.println("C: " + t.getInstances(appearance, "C"));
System.out.println("D: " + t.getInstances(appearance, "D"));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |