数组(toString)无法正确输出

时间:2010-06-14 20:51:31

标签: java

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

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

2 个答案:

答案 0 :(得分:0)

使用以下代码创建一个字符串,该字符串代表整个数组。然而,仅仅使用数组'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;

答案 1 :(得分: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
相关问题