java中替代table_Java中Hashtable代替C#中Dictionary的用法

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的是哪个类?附上一段小例子?

谢谢!!

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值