Which is the correct way to refer to the first element of an ArrayList?

How to get first and last elements from ArrayList in Java?

JavaObject Oriented ProgrammingProgramming

The get() method of the ArrayList class accepts an integer representing the index value and, returns the element of the current ArrayList object at the specified index.

Therefore, if you pass 0 to this method you can get the first element of the current ArrayList and, if you pass list.size()-1 you can get the last element.

Get first and last elements from ArrayList in Java

Given an array list, find the first and last elements of it.

Examples:

Input : aList = {10, 30, 20, 14, 2} Output : First = 10, Last = 2 Input : aList = {10, 30, 40, 50, 60} Output : First = 10, Last = 60

First and Last elements from ArrayList in Java

Here is a code example to get both the first and last elements from ArrayList in Java. It's pretty straightforward, all you need to do is call get(0) to get the first element and call get(size() - 1) to retrieve the last element.




In this example, we have an ArrayList of video games, some of the super hit titles of Nintendo era, first element is "Super Mario Bros" while last game is "Race". Let's see how we retrieved these two objects.

import java.util.Arrays; import java.util.List; /** * Java Program to find first and last elements from Java ArrayList. * * @author WINDOWS 8 */ public class FirstFromArrayList { public static void main(String args[]){ // let's create ArrayList of String elements // Arrays.asList() is a shortcut to create and initialize // ArrayList in same line List<String> games = Arrays.asList("Super Mario Bros", "MK3", "Brian Lara Cricket", "Donky Kong", "Race"); // now let's find out first and last elements of ArrayList // first element is always on 0 index // last element is at size - 1 index String first = games.get(0); String last = games.get(games.size() -1); System.out.println("ArrayList : " + games); System.out.println("First element in arraylist : " + first); System.out.println("Last element in arraylist : " + last); } } Output ArrayList : [Super Mario Bros, MK3, Brian Lara Cricket, Donky Kong, Race] First element in arraylist : Super Mario Bros Last element in arraylist : Race
You can see that our program has correctly retrieved the first and last element from the ArrayList in Java.

That's all on how to get the first and last element from ArrayList in Java. Java Collection API does provide method to access ArrayList elements by index, these methods are actually defined inside the List interface.

If you are interested to learn more about ArrayList, check out these interesting tutorials and interview questions :
  • How to sort ArrayList in descending order in Java? (answer)
  • How to synchronize ArrayList in Java? (answer)
  • Top 20 ArrayList Interview Questions for Java Beginners (list)
  • When to use ArrayList and LinkedList in Java? (answer)
  • Difference between ArrayList and HashSet in Java? (answer)
  • Difference between an array and ArrayList in Java? (answer)
  • How to remove elements from ArrayList in Java? (answer)
  • Difference between HashMap and ArrayList in Java? (answer)
  • What is difference between fail-safe and fail-fast Iterator? (answer)
  • Difference between length() of array and size() of ArrayList in Java? (answer)
  • How to convert ArrayList to String in Java? (answer)
  • How to remove duplicates from ArrayList in Java? (solution)
  • Difference between Vector and ArrayList in Java? (answer)

1. ArrayList get() Method

ArrayList.get(int index) method returns the element at the specified position 'index' in the list.

1.1. Syntax

public Object get( int index );

1.2. Method Parameter

index – index of the element to return. A valid index is always be between 0 (inclusive) to the size of ArrayList (exclusive).

For example, if ArrayList holds 10 objects then a valid argument index will be between 0 to 9 (both inclusive).

1.3. Return Value

The get() method returns the reference of the object present at the specified index.

1.4. IndexOutOfBoundsException

An invalid index argument will cause IndexOutOfBoundsException error.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at com.howtodoinjava.example.ArrayListExample.main(ArrayListExample.java:12)

How to get the first element of an ArrayList in Java

java1min read

In this tutorial, we are going to learn about how to get the first element of an ArrayList in Java.

Consider, we have a following ArrayList:

List<Integer> list = Arrays.asList(10, 20, 30, 40);

To get the first element of a ArrayList, we can use the list.get() method by passing 0 as an argument.

0 is refers to first element index.

Here is an example, that returns the first element 10 from the following ArrayList:

import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(10, 20, 30, 40); System.out.println(list.get(0)); } }

Output:

10
  • Share:

Video liên quan

Postingan terbaru

LIHAT SEMUA