简体   繁体   English

Java中的地图输入键

[英]Map entry key in java

I'm trying to debug a problem I've on a script, I'm newly to Java I think it's a simplest thing but I need to understand. 我正在尝试调试脚本中的问题,我刚接触Java,我认为这是最简单的事情,但我需要了解。 This : 这个 :

for( Map.Entry<String,int[]> entry : this.indexMap.entrySet())
{
     if( !entry.getKey().equals("nickname"))
     {
         System.out.print("'"+ entry.getKey() +"' contains "+ entry.getKey().length() +" chars");
         System.out.print("'"+ name +"' contains "+ name.length() +" chars");
     }
     else if( entry.getKey().trim().equals("nickname") )
     {
         System.out.print("Yes are sames");
     } 
}

For a String name = "nickname", displays me that : 对于字符串名称=“昵称”,向我显示:

18:56:15 [INFOS] 'nickname' contains 94 chars

18:56:15 [INFOS] 'nickname' contains 8 chars

I'm trying to understand this. 我正试图了解这一点。

The problem is entry.getKey() returns the same thing as my string name, but not really the same. 问题是entry.getKey()返回与我的字符串名称相同的东西,但并不完全相同。 In first test, we saw the two vars are different, so the print is did, but the twos vars have the same value, and not the same length. 在第一个测试中,我们看到两个变量不同,因此可以进行打印,但是两个变量具有相同的值,但长度不同。 In the else-if, I tried to remove spaces but not printed so where are from these 94 chars? 在else-if中,我尝试删除空格但未打印,所以这94个字符在哪里?

https://code.google.com/p/imdbparsers/source/browse/trunk/imdb+parsers/src/imdb/parsers/xmltosql/NamedParameterStatement.java?r=6 https://code.google.com/p/imdbparsers/source/browse/trunk/imdb+parsers/src/imdb/parsers/xmltosql/NamedParameterStatement.java?r=6

Is the code, methods concerned are 是代码,有关方法是

private String parse(String query) 

private int[] getIndexes(String name)

line 161 et 89 This for loop i've in mine is only to debug the 第161等第89行我在我的这个for循环中仅用于调试

 int[] indexes = (int[]) indexMap.get(name);

Returns always null 返回始终为null

The query string is : 查询字符串是:

SELECT COUNT(`account_id`) AS `total` FROM `game_accounts` WHERE `nickname`=:nickname

The difference between 和...之间的不同

entry.getKey().equals("nickname")

and

entry.getKey().trim().equals("nickname")

is trim() . trim()

The first take in account the spaces and the second not. 第一个考虑空格,第二个不考虑空格。

It's because they are a loop on a map: to find the 'bad' keys... 这是因为它们是地图上的一个循环:查找“坏”键...

I think that if you reverse your if clauses you might get something that behaves more like what you are expecting, although it is somewhat unclear what you are asking. 我认为,如果您颠倒if子句,您可能会得到一些更像您期望的行为,尽管不清楚您在问什么。 Comparing keys as the first clause in the if block makes the code simpler. 将键作为if块中的第一个子句进行比较会使代码更简单。

if( entry.getKey().trim().equals("nickname") ) 
{
     System.out.print("Yes are sames");
}
else
{
     System.out.print("'"+ entry.getKey() +"' contains "+ entry.getKey().length() +" chars");
     System.out.print("'"+ name +"' contains "+ name.length() +" chars");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM