usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceConsoleApplication2{classProgram{staticvoidMain(string[]args){Aa=newA();a.Test("11133");Console.Read...
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
A a = new A();
a.Test("11133");
Console.Read();
}
}
class A
{
public void Test(string s)
{
string st = "";
int count = 0;
string st1="";
Dictionary table = new Dictionary();
table.Add(s.Substring(0,1),1);
for (int i = 1; i < s.Length; i++)
{
st = s.Substring(i, 1);
if (table.ContainsKey(st))
{
table[st] = table[st] + 1;
}
else
{
table.Add(st, 1);
}
if (table[st] > count)
{
count = table[st];
st1 = st;
}
}
Console.Write(st1 + "出现了" + count);
}
}
}
在c#中求一个字符串中出现最多的字符数,用dictionary
但是用java中的HasTable我就不会了
用一层循环
关键是这个方法
table[st] = table[st] + 1;
Hastable中的get()不能完全替代。求高手解决!
在线等
既然Hashtable不合适,那么能完全替代C#中Dictionary的是哪个类
不过用Hashtable研究了大半天算是出来了
import java.util.*;
class A
{
void Test(String s)
{
String c="";
int count=0;
String c1="";
Hashtable table =new Hashtable();
table.put(s.substring(1,1),1);
for(int i=1;i
{
c=s.substring(i,i+1);
if(table.containsKey(c))
{
table.put(c,table.get(c)+1);
}
else
table.put(c,1);
if(table.get(c)>count)
{
count=table.get(c);
c1=c;
}
}
System.out.println(c1+"出现了"+count);
}
};
class Test
{
public static void main(String[] a1)
{
A a=new A();
a.Test("145151847877433");
}
}
决定性的回答就给分
也就是这个问题:既然Hashtable不合适,那么能完全替代C#中Dictionary的是哪个类?附上一段小例子?
谢谢!!
展开