简体   繁体   English

如何从ArrayList中获取元素?

[英]How can I get elements from the ArrayList?

I have 我有

private List<Test> tests   = new ArrayList<Test>();

and

void creatingList ()
{
    for (int i = 0; i < 10; i++)
        for (int j = 0; j < 10; j++) {
            if (array[k] == 1)
            {
                Test te = new Test(i, j);
                tests.add(te);
            }
            k++;
        }
}

For example, I have here list of numbers or Strings: (1,0); 例如,我这里有数字或字符串列表:(1,0); (1,1); (1,1); (3,5); (3,5); (5,5) (5,5)

How can I get elements at specified position in the List? 如何获取元素在列表中的指定位置? For example, I need second value from the element included in the 3rd parenthesis. 例如,我需要第三个括号中包含的元素的第二个值。

To get element at specified index in the List one shoud use get(int index) method. 要获取List中指定索引处的元素,请使用get(int index)方法。

In your case you may want to use: tests.get(2).nameOfTheFieldWhereTheSecondValueIsStored. 在您的情况下,您可能需要使用: tests.get(2).nameOfTheFieldWhereTheSecondValueIsStored.

It is strongly recommended, to have a glance at documentation before asking a question: http://docs.oracle.com/javase/7/docs/api/java/util/List.html 强烈建议在提出问题之前先浏览一下文档: http : //docs.oracle.com/javase/7/docs/api/java/util/List.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM