How to use ArrayList's get() method

后端 未结 5 2001
长发绾君心
长发绾君心 2021-01-04 13:27

I\'m new to java (& to OOP too) and I\'m trying to understand about the class ArrayList but I don\'t understand how to use the get(). I tried searching in net, but could

5条回答
  •  天命终不由人
    2021-01-04 13:51

    You use List#get(int index) to get an object with the index index in the list. You use it like that:

    List list = new ArrayList();
    list.add(new ExampleClass());
    list.add(new ExampleClass());
    list.add(new ExampleClass());
    ExampleClass exampleObj = list.get(2); // will get the 3rd element in the list (index 2);
    

提交回复
热议问题