开发者

Arrays (toString) not output correctly

Actually 开发者_如何学Gothis tread is continuing from the other one. There wasn't enough characters to continue there. Anyway. the problem is that the output is "1(10) 2(23) 3(29)". Even though I could return string for the array values (10,23,29) and used string reference as 1, 2 and 3. My question is it possible to return index values 1,2,3 and as well as array values. Am I making an sense. Here is what I have done...

// 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;


With the below code you are creating a string that and this string represents the whole array. Yet it will be harder to work with then just using the array 'groups'

 // 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;


If you create a Map, and save your data in there, you can get any information you want. Like:

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

After storing everything, you can get the values of the Map by its key or value. Example:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜