有一个数组,String [] a={ads,awww,fdgfdf,fvdsfc,hyjutfrdf},
题目要求,例如,数组a={ads}中,在a-z共二十六个字母中,字母a出现了1次,d出现了1次,s出现了1次,其他字母出现了0次,求出在以上数组中,打印出各个字母都出现了多少次。
呵呵,求高人给大家解答分享下。
题目要求,例如,数组a={ads}中,在a-z共二十六个字母中,字母a出现了1次,d出现了1次,s出现了1次,其他字母出现了0次,求出在以上数组中,打印出各个字母都出现了多少次。
呵呵,求高人给大家解答分享下。
解决方案 »
- 一道面试题,求助!
- 请问这个用xpath如何提取?
- java跟access日期查询问题
- 在html页面中运行applet,为何不成功,提示:class not found(跟贴有分)
- JComboBox 自动提示功能问题!!
- 我在JAVA的UI上做了新尝试,不要再说JAVA做不出好看的界面了
- 如何恰当地获得已运行时间
- 接口引用了其他的类吗
- -----------请问各位,有没有用JBUILDER操作数据库的实例原码,不是用组件-------时刻在线等待!!!------
- jdk配置
- 麻烦各位大神看看,是哪里出错了,小弟初学者。
- 特定的字符串的查找,比如在一段字符串中查找for后面的判断语句while,do while, else if,if 后面的判定语句
public class Hello {
public static void main(String[] args) {
String [] a = {"ads", "awww", "fdgfdf", "fvdsfc", "hyjutfrdf"};
int[] frequence = new int[256]; for (String str : a) {
for (int i = 0; i < str.length(); ++i) {
frequence[str.charAt(i)]++;
}
} for (int i = 0; i < frequence.length; ++i) {
if (frequence[i] != 0) {
System.out.printf("%s: %d\n", (char)i, frequence[i]);
}
}
}
}
a: 2
c: 1
d: 5
f: 7
g: 1
h: 1
j: 1
r: 1
s: 2
t: 1
u: 1
v: 1
w: 3
y: 1
import java.util.Map;
import java.util.TreeMap;public class StringCount
{
public static void main(String[] args)
{
String[] arr ={"ads","awww","fdgfdf","fdgfdf","fdgfdf"};
System.out.println(getCount(arr));;
}
private static String getCount(String[] strs)
{
StringBuilder builder = new StringBuilder();
for(int x=0; x<strs.length; x++)
{
builder.append(strs[x]);
}
String s = builder.toString();
char[] arr = s.toCharArray();
Map<Character,Integer> map = new TreeMap<Character,Integer>();
int count = 1;
for(int x=0; x<arr.length; x++)
{
if(!(arr[x]>='a' && arr[x]<='z' || arr[x]>='A' && arr[x]<='Z'))
continue;
if(!(map.containsKey(arr[x])))
map.put(arr[x], count);
else
count = map.get(arr[x]) + 1;
map.put(arr[x], count);
count = 1;
}
StringBuilder sb = new StringBuilder();
Iterator<Map.Entry<Character, Integer>> it = map.entrySet().iterator();
while(it.hasNext())
{
Map.Entry<Character, Integer> me = it.next();
char key = me.getKey();
int value = me.getValue();
sb.append(key+":("+value+")");
}
return sb.toString();
}
}
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] s = {"ads","awww","fdgfdf","fvdsfc","hyjutfrdf"};
int[] len = new int[26];
for(int i = 0;i < s.length;i++){
for(int j = 0;j <s[i].length();j++){
len[s[i].charAt(j) - 'a']++;
}
}
for(int i = 0;i < 26;i++){
char temp;
temp = (char)('a' + i);
System.out.print(temp);
System.out.println(":"+len[i]);
} }}
仿照Thanking in Java的例子写的public class Test {
public static void main(String[] args) {
String[] a={"ads","awww","fdgfdf","fvdsfc","hyjutfrdf"};
Map<String,Integer>map=new HashMap<String,Integer>();
for(int n=0;n<a.length;n++){
for(int i=0;i<a[n].length();i++){
Integer fre=map.get(String.valueOf(a[n].charAt(i)));
map.put(String.valueOf(a[n].charAt(i)), fre==null ? 1:fre+1);
}
}
System.out.println(map);
}
}运行结果{f=7, g=1, d=5, c=1, a=2, j=1, h=1, w=3, v=1, u=1, t=1, s=2, r=1, y=1}
public static void main(String[] args) { String[] a = { "ads", "awww", "fdgfdf", "fvdsfc", "hyjutfrdf" };
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (int n = 0; n < a.length; n++) {
for (int i = 0; i < a[n].length(); i++) {
Integer fre = map.get(a[n].charAt(i));//不存在返回null否则返回当前出现的次数
map.put(a[n].charAt(i), fre == null ? 1 : fre + 1);
}
}
System.out.println(map);
}
}