2010-06-14 78 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