2014-01-06 25 views
0

我想说,我有1天的Java体验,因此如果它有点容易的问题,对此感到抱歉。字符串数组的打印内容java

现在,我想展示我的一段代码。

我有一个字符串数组,如:

String [] attributeNames[4]; 

我读过4名的每一个为从二进制文件64个字节,并将它们存储到该阵列。 (属性名称包含空端字符串最多64个字节),然后我想不过来打印我的attributeNames阵列和它们的长度,如果我写:

for(int i=0;i<attributeNames2.length;i++){ 
    System.out.println(
     attributeNames2[i] + 
     " length: "+attributeNames2[i].length() 
    ); 
} 

我只拿到了

pathway    
shortest_path 
path_length 
avg_neighbour_number 

结果。长度不打印,但如果我写

for(int i=0;i<attributeNames2.length;i++){ 
    System.out.println(
     "length:"+attributeNames2[i].length() + 
     "names:"+ attributeNames2[i] 
    ); 
} 

length: 64 pathway 
length: 64 shortest_path 
length: 64 path_length 
length: 64 avg_neighbour_number 

我不知道为什么?有人帮我吗?

+1

正在创建一个2维数组的第一次尝试.. – TheLostMind

+0

输出不对应于代码(不编译) - 显示你真实的代码。 – assylias

+1

@TheLostMind执行“string [] identifier [x];”编译为“string [] [x]标识符”? (真正的问题,因为我从来没有写过Java) –

回答

1

你的循环看起来不错,问题可能在于你创建数组的方式。正确的做法是

String[] attributeNames2 = new String[4]; 
attributeNames2[0] = "pathway"; 
//and so on 

或者你可以创建静态数组,这似乎足够我在你的情况

String[] array = {"pathway", "shortest_path", "and so on"}; 
+0

attributeNames [0]将不仅仅是“路径”,相反,因为我读64个字节,从路径到64个字节会有空格,我需要删除它们,我想 – TheGost