GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing www.geeksforgeeks.org — ArrayList get(index) Method in Java with Examples - GeeksforGeeks ArrayList get(index) Method in Java with ExamplesLast Updated : 22 Jul, 2026 The get(int index) method of the ArrayList class is used to retrieve the element present at the specified index in the list. Since ArrayList stores elements in insertion order and supports random access, the get() method provides fast retrieval of elements by their position.
Output Element at index 1: Banana Explanation: In the above example, an ArrayList containing three fruits is created. The get(1) method returns the element present at index 1, which is Banana. Syntax
Example: Accessing Elements Using get() Inside a Loop
Output Element at index 0 = 10 Element at index 1 = 20 Element at index 2 = 30 Element at index 3 = 40 Explanation: Here, the get() method is used inside a loop to retrieve each element from the ArrayList using its index. Example: Handling Invalid Index
Output: Exception in thread "main" Explanation: The ArrayList contains only 3 elements (indices 0 to 2). Attempting to access index 5 results in an IndexOutOfBoundsException. get() Vs elementAt()
Advantages of get() Method
Comment Explore | ||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |