Java中基于索引的数据结构
这个问题在这里已经有了答案:
如果我理解你的问题,你可以像这样使用List<String>
public static void main(String[] args) {
List<String> al = Arrays.asList(":a", ":a", ":b",
":c", ":f", ":f");
for (int i = 0; i < al.size(); i++) {
System.out.printf("%d. %s%n", 1 + i, al.get(i));
}
}
输出是(注意:索引从0开始,所以我添加了一个以获得您请求的输出)
1. :a
2. :a
3. :b
4. :c
5. :f
6. :f
你可以使用array
,数组是一个可以有很多值的对象,你可以使用索引访问。
例:
int a = 10;
int b = 11;
int c = 12;
int d = 13;
int value[] = { a, b, c, d };
//syntax access is value[elementPosition], you have 4 elements inside, positions begins on zero, so you must use 0, 1, 2 and 3 (total of 4).
//value[0] will result 10
//value[1] will result 11
//value[2] will result 12
//value[3] will result 13
你可以使用:
int value[] = { 10, 11, 12, 13 };
int[] value = { 10, 11, 12, 13 };
或者您可以创建一个数组并稍后传递它的值:
int[] value = new int[4] // 4 = num of values will be passed to this array
value[0] = 10;
value[1] = 11;
value[2] = 12;
value[3] = 13;
value[4] = 14 // this will result IndexOutOfBoundsException, because it will be the 5th element.
您可以对String,Float等执行相同的操作。
链接地址: http://www.djcxy.com/p/19973.html