Regarding the heading of your question:

get first and last element in ArrayList in Java

It should be pretty simple:

mimeList.get(0); // To get first
mimeList.get(mimeList.size()-1); //to get last

And regarding your if condition :

if(!(i==0 || i==mimeList.size()-1))

As you phrased it like:

if the element in mimeList is first or last it will go in else condition other wise in if condition

I used ! in if condition. Otherwise below is pretty cool:

if((i>0) && (i!=mimeList.size()-1))
Answer from Priyank Doshi on Stack Overflow
🌐
Medium
medium.com › @manishkumar.modi › why-java-21-added-getfirst-and-friends-to-arraylist-even-though-they-just-call-old-methods-d08c9d4e4b6c
🧭 Why Java 21 Added getFirst() and Friends to ArrayList — Even Though They Just Call Old Methods | by Manishkumar Modi | Medium
April 14, 2026 - Yes, you heard it right, and that’s completely fine. If you peek into the source code of ArrayList Java 21, you’ll find: public E getFirst() { return get(0); } public E getLast() { return get(size() - 1); }
🌐
GeeksforGeeks
geeksforgeeks.org › java › get-first-and-last-elements-from-arraylist-in-java
Get first and last elements from ArrayList in Java - GeeksforGeeks
July 11, 2025 - import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class FindFirstLast { public static void getFirstLast(List<Integer> list) { if (list == null || list.isEmpty()){ return; } Iterator<Integer> iterator = list.iterator(); // Get the first element int first = iterator.next(); int last = first; // Traverse the remaining elements while (iterator.hasNext()){ last = iterator.next(); } System.out.println("First element: " + first); System.out.println("Last element: " + last); } public static void main(String[] args){ List<Integer> list = new ArrayList<>(); list.add(12); list.add(24); list.add(36); list.add(48); list.add(60); System.out.println("ArrayList: " + list); getFirstLast(list); } } Output ·
🌐
Level Up Lunch
leveluplunch.com › java › examples › get-first-element-in-list
Get first element in list | Level Up Lunch
January 31, 2014 - @Test public void get_first_el... firstElement.orElse("two")); } Guava Iterables.getFirst will return the first element in the list or the default value if the list is empty....
🌐
Baeldung
baeldung.com › home › java › java collections › guide to the java arraylist
Guide to the Java ArrayList | Baeldung
December 14, 2024 - Let’s discuss with examples how to perform the add/get/remove operations on an ArrayList that’s a sequenced collection. To get the first element, we call the instance method getFirst().
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-get-first-and-last-elements-from-arraylist-in-java
How to get first and last elements from ArrayList in Java?
September 1, 2025 - 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.
Find elsewhere
🌐
Java67
java67.com › 2015 › 06 › how-to-get-first-and-last-elements-form-ArrayList-java.html
How to get first and last elements form ArrayList in Java | Java67
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.
🌐
Learn Java
javatutoring.com › java-get-first-element-array
Java : Return/Get First Element In Array List | 4 Ways
April 12, 2026 - Get First Element Of ArrayList · This code is for displaying or printing out the first element of a user-defined array. The problem here is to print out the first element in an array whose elements are input by the user.
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-the-first-element-of-the-list-in-java
How to get the first element of the List in Java?
May 19, 2025 - import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class findFirstElement { public static void main(String[] args) { //creating a list List<Integer> list = new ArrayList<>(); //adding element to it list.add(1); list.add(2); list.add(3); list.add(4); System.out.println("The list elements are: " + list); //Getting an Iterator Object Iterator<Integer> it = list.iterator(); if(it.hasNext()){ //using the next() method System.out.println("The first element in the list is: " + it.next()); } } }
🌐
Reactgo
reactgo.com › home › how to get the first element of an arraylist in java
How to get the first element of an ArrayList in Java | Reactgo
August 22, 2023 - To get the first element of a ArrayList, we can use the list.get() method by passing 0 as an argument.
🌐
GitHub
github.com › phax › ph-commons › issues › 42
Java SE 21 adds new ArrayList methods conflicting with `ICommonsList` · Issue #42 · phax/ph-commons
March 26, 2024 - ArrayList.getFirst() ArrayList.getLast() ArrayList.removeFirst() ArrayList.removeLast() These methods in ICommonsList need to be deprecated for removal and instead new methods with OrNull appended to their names, but with the same functionality needs be added.
Author: phax
🌐
GitHub
github.com › jruby › jruby › issues › 8526
obj.first caused Java::JavaUtil::NoSuchElementException on an empty Java21 ArrayList · Issue #8526 · jruby/jruby
December 16, 2024 - Java21 introduced List.getFirst() and getLast(). So now this happens only in Java21 (jruby-9.4.9.0) because JRuby is calling the Java method. The same code runs fine and returns nil in Java17 · irb(main):004:0> a = java.util.ArrayList.new => #<Java::JavaUtil::ArrayList: []> irb(main):005:0> a => #<Java::JavaUtil::ArrayList: []> irb(main):006:0> a.first java.util.ArrayList.getFirst(java/util/ArrayList.java:439): Java::JavaUtil::NoSuchElementException from jdk.internal.reflect.DirectMethodHandleAccessor.invoke(jdk/internal/reflect/DirectMethodHandleAccessor.java:103) from java.lang.reflect.Meth
Author: jruby
🌐
Oracle
docs.oracle.com › en › java › javase › 22 › docs › api › java.base › java › util › ArrayList.html
ArrayList (Java SE 22 & JDK 22) - Oracle
July 16, 2024 - The size, isEmpty, get, set, getFirst, getLast, removeLast, iterator, listIterator, and reversed operations run in constant time. The add, and addLast operations runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation. Each ArrayList instance has a capacity.
🌐
TWpower's Tech Blog
twpower.github.io › 328-difference-between-get-and-findfirst-in-java-list-en
[Java](EN) Difference between get(0) and findFirst() in ...
November 19, 2023 - Return the first element that meets the given condition and if there is no element satisfying that condition then return Optional.empty() · List<String> myList = Arrays.asList("1", "2", "3"); Optional<String> firstElementOptional = ...