我试着保持这个简单,但仍然可以理解.如果有任何困惑,请告诉我!
我得到了这个哈希表:
private Hashtable<String, ArrayList<String>> allChannels = new Hashtable<String, ArrayList<String>>();
Run Code Online (Sandbox Code Playgroud)
我正在使用此方法插入客户端/用户:
public void connectChannel(String username, String channel) throws RemoteException{
allChannels.put(channel, new ArrayList<String>());
allChannels.get(channel).add(username);
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用这个方法,以获得一个连接到给定通道(参数)的用户的arraylist.怎么可以这样做?
@Override
public ArrayList<String> getUsersInChannel(String channel) throws RemoteException{
return **Code needed**
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
编辑:顺便说一句,连接方法得到了几个检查,但它很长,所以我没有打扰它,因为我知道它的工作很好.
编辑:因为使用return allChannels.get(channel)似乎是正确的,所以我的JList还有其他问题.我在GUILogic中使用了这些方法来运行JList.我已经使另外两个JList以相同的方式工作了.
DefaultListModel usersInChanDLM = new DefaultListModel();
public DefaultListModel getUsersInChannelAsDefaultListModel() throws RemoteException{
if(!(getChannel() == null)){
for(String a : cf.getUsersInChannel(getChannel())){
usersInChanDLM.addElement(a);
System.out.println(a);
}
}
return usersInChanDLM;
}
void updateUsersInChannelJlist(JList jList3) throws RemoteException {
usersInChanDLM.clear();
usersInChanDLM.removeAllElements();
for(Client c : cf.getClients()){
if(!(usersInChanDLM.contains(c.findName()))){
usersInChanDLM.addElement(c.findName());
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个服务器IP地址列表,我的应用程序与之交互以收集实时数据.收集的数据被发送到另一个系统的插槽.这些槽由八个字符长度的字符串标识.我想以编程方式创建这个插槽.我想创建一个方法,将服务器的IP地址映射到八个字符串.例如.给定192.80.24.200它将返回SLOT0001我假设散列函数将是一个很好的解决方案,但我不确定它是如何实现的.
该应用程序是C#Winforms .Net Framework 4.0.服务器是基于Windows的服务器.
我对Java Collection对象有一些疑问......
HashSet或者HashMap如何在内部存储对象?Hashtable不允许null值?我有一个合并的哈希表,它有大约4351个不同的键及其相关值,但我不想指定其每个键来获取值,我想直接收集所有值,我如何处理这个?
注意:所有值都是100%不同,这就是为什么我想盲目地获取这些数据以进行进一步处理
有人能告诉我这是什么@{hash{$key}}意思吗?当我写作时我是否是对的@{hash{$key}}[$i]=$list[$i+1]?
由于在Java中实现接口的类必须定义接口中的每个方法以避免被声明为抽象,我想知道以下内容:
当我创建一个实例化Hashtable对象的程序时,为什么我不需要在Map接口中定义每个方法?我没有明确定义的方法是自动创建的"存根"吗?
Hashtable ht = new Hashtable();
for (int i = 0; i < 100; i++) {
ht.put(i%10, i);
}
Enumeration< Integer> eles = ht.elements();
while(eles.hasMoreElements())
System.out.println(eles.nextElement());
Run Code Online (Sandbox Code Playgroud)
上面的代码片段是打印99,98,....... 90
但我想要打印所有100个元素.如何获得一个数字列表,如... 99,89,79,69,... 19,9 98,88,78,68 .... 18,8 97,87,77,67 .... 17,7 .. .. 91,81,71,61 .... 11,1
基本上都是碰撞清单.
编辑:FML!我对哈希码的实现有一个小写的c.-.-
我一直在努力学习TDD并一直关注Kent Beck的"By Example"一书; 这很好!
但是,我似乎无法进步,因为当我访问哈希表时,值返回null.我已经运行了一个调试会话,并且具有该值的对象显然存在,但结果为null.
构建和访问的代码是:
public void addRate(String from, String to, int rate){
this.rates.put(new Pair(from, to), new Integer(rate));
}
Run Code Online (Sandbox Code Playgroud)
来自和来自"GBP"和"USD".也通过调试验证.
调用上面的测试用例:
@Test
public void testreduceMoneyDifferentCurrency(){
Bank bank = new Bank();
bank.addRate("GBP", "USD", 2);
Money result = bank.reduce(Money.gbpound(2), "USD");
assertEquals(Money.dollar(1), result);
}
Run Code Online (Sandbox Code Playgroud)
银行中的reduce方法调用方法速率:
public Money reduce(Bank bank, String to){
int rate = bank.rate(this.currency, to);
return new Money(this.amount / rate, to);
}
Run Code Online (Sandbox Code Playgroud)
问题出在哪里:
public int rate(String from, String to){
if (from.equals(to)) return 1;
Integer rate = (Integer) this.rates.get(new Pair(from, to)); …Run Code Online (Sandbox Code Playgroud) 我有以下字典.
var = a = {
'Black': { 'grams': 1906, 'price': 2.05},
'Blue': { 'grams': 9526, 'price': 22.88},
'Gold': { 'grams': 194, 'price': 8.24},
'Magenta': { 'grams': 6035, 'price': 56.69},
'Maroon': { 'grams': 922, 'price': 18.76},
'Mint green': { 'grams': 9961, 'price': 63.89},
'Orchid': { 'grams': 4970, 'price': 10.78},
'Tan': { 'grams': 6738, 'price': 50.54},
'Yellow': { 'grams': 6045, 'price': 54.19}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能根据它进行排序price.因此,生成的字典将如下所示.
result = {
'Black': { 'grams': 1906, 'price': 2.05},
'Gold': { 'grams': 194, 'price': 8.24}, …Run Code Online (Sandbox Code Playgroud) 我试图迭代HashTable并更新值,只要它通过一些条件.这是代码.
public static Hashtable AssembliesGlobal= new Hashtable();
public static void populateHash(){
NDepend.Path.IAbsolutePath assemblyName;
NDepend.Path.IAbsolutePath projectName;
for (int i = 0; i < someList.Count(); i++)
{
if (someList[i].AssembliesUsed.Count() > 0)
{
assemblyName = getAssemblyQuery[i].a.FilePath;
if (getAssemblyQuery[i].a.VisualStudioProjectFilePath != null)
{
List<IAbsolutePath> thirdPartyList = new List<IAbsolutePath>();
projectName = getAssemblyQuery[i].a.VisualStudioProjectFilePath;
thirdPartyList.Add(assemblyName);
AssembliesGlobal.Add(projectName, thirdPartyList);
}
}
}
}
public static void parseCsproj()
{
foreach (IAbsoluteFilePath key in AssembliesGlobal.Keys)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(key.FileInfo.FullName);
XmlNodeList references = xmldoc.GetElementsByTagName("ProjectReference");
XmlNodeList hintReferences = xmldoc.GetElementsByTagName("HintPath");
if (references.Count …Run Code Online (Sandbox Code Playgroud)