2010-06-14 79 views
-3

实际上这个步距是从另一个继续。没有足够的角色继续在那里。无论如何。问题是输出是“1(10)2(23)3(29)”。即使我可以返回字符串的数组值(10​​,23,29)和使用字符串引用作为1,2和3.我的问题是有可能返回索引值1,2,3以及数组值。我有意义吗?这是我所做的...阵列(toString)输出不正确

// int[] groups = {10, 23, 29}; in the constructor 

String tempA = ""; 
String tempB = " "; 

int[] temp = new int[4]; 
int length = groups.length; 

for (int j = 0; j < length; j++) 
{ 
    temp[j] = groups[j]; 
    tempB = tempB + "("+goups+")"; 
} 

groups = temp; 
Arrays.sort(coingroups); 

for(int i = 1; i < groups.length;i++) 
{ 
    tempA = tempA+" "+(i)+ "("+groups[i]+")"; 
} 
return tempA; 
+0

什么是良好(预期)输出的例子? – Sev 2010-06-14 20:53:20

+0

什么意思Sev? – DiscoDude 2010-06-14 20:55:04

+1

@dowln:你想要输出什么? – 2010-06-14 20:55:43

回答

0

用下面的代码,你正在创建一个字符串,这个字符串代表整个数组。然而,这将是很难与当时只是用工作数组“组”

// int[] groups = {10, 23, 29}; in the constructor 
String tempA = ""; 
for (int j = 0; j < groups.length; j++) 
{ 
    tempA = tempA + " " + j + " (" + groups[j] + ") "; 
} 
return tempA; 
0

如果你创建一个地图,并保存在那里你的数据,你可以得到你想要的任何信息。如:

Map<Integer, String> stack = new HashMap<Integer, String>(); 
stack.put(1, "10"); 
stack.put(2, "23"); 
stack.put(3, "29"); 

存储所有内容后,您可以通过其键或值获取Map的值。例如:

stack.get(1) will return "10" 
stack.get("10") will return 1