简体   繁体   中英

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,1); (3,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.

In your case you may want to use: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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