Java programming language provides List and ArrayList classes and objects in order to store multiple items in a single variable or structure. These List and ArrayList objects provide the get()
method which is simply used to get an item from the List or ArrayList.
Java编程语言提供List和ArrayList类和对象,以便将多个项目存储在单个变量或结构中。 这些List和ArrayList对象提供get()
方法,该方法仅用于从List或ArrayList获取项目。
get()方法语法 (get() Method Syntax)
The get()
method of the ArrayList or List has the following syntax. The index numbers in Lists and ArrayLists start from 0 which means the first item or element index number is 0.
ArrayList或List的get()
方法具有以下语法。 列表和数组列表中的索引号从0开始,这意味着第一项或元素的索引号为0。
ITEM OBJECT.get(int INDEX)
ITEM
is the object which will be returned from the given OBJECT with the given INDEX number.ITEM
是将从给定对象以给定索引号返回的对象。OBJECT
is the list or ArrayList object which contains items.OBJECT
是包含项的列表或ArrayList对象。int INDEX
specifies the index number of the item we want to get to ITEM from the OBJECT.int INDEX
指定我们要从OBJECT获取到ITEM的项目的索引号。
mylist.get(5)
mylist.get(5)
mylist
is an ArrayList or List which contains multiple items.mylist
是一个包含多个项目的ArrayList或List。get(5)
will return the item which index is 5.get(5)
将返回索引为5的项目。
ArrayList的get()方法示例 (get() Method Example with ArrayList)
We will make an example with ArrayList and get()
method. We will create an ArrayList named mylist
and add items 10,20,30,40,50,60 to this list. Then we will use get() function to return the 2nd element of the ArrayList.
我们将使用ArrayList和get()
方法作为示例。 我们将创建一个名为mylist
的ArrayList并将10,20,30,40,50,60项添加到此列表中。 然后,我们将使用get()函数返回ArrayList的第二个元素。
import java.util.*;
public class Java_List_Get {
public static void main(String[] args)