Get ArrayList elements

We can use get(int index) method for retrieving the element from list

It returns the value present at the specified index

This method throws IndexOutOfBoundsException if the index is less than zero or greater than the size of the list

Example 1 :

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class ArrayListGetElement {
  4.     public static void main(String[] args) {
  5.     ArrayList<String> list = new ArrayList<String>();
  6.     list.add("java");
  7. list.add("c++");
  8. list.add("python");
  9. list.add("c");
  10. System.out.println("First element of the ArrayList: "+list.get(0));
  11. System.out.println("Third element of the ArrayList: "+list.get(2));
  12.     }
  13. }
import java.util.ArrayList;
import java.util.List;
public class ArrayListGetElement {
	public static void main(String[] args) {
	ArrayList<String> list = new ArrayList<String>();
	list.add("java");
list.add("c++");
list.add("python");
list.add("c");
System.out.println("First element of the ArrayList: "+list.get(0));
System.out.println("Third element of the ArrayList: "+list.get(2));
	}
}



Example 2 :

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class ArrayListGetElement {
  4.     public static void main(String[] args) {
  5.     ArrayList<String> list = new ArrayList<String>();
  6.     list.add("java");
  7. list.add("c++");
  8. list.add("python");
  9. list.add("c");
  10. System.out.println("First element of the ArrayList: "+list.get(0));
  11. System.out.println("Third element of the ArrayList: "+list.get(5));
  12.     }
  13. }
import java.util.ArrayList;
import java.util.List;
public class ArrayListGetElement {
	public static void main(String[] args) {
	ArrayList<String> list = new ArrayList<String>();
	list.add("java");
list.add("c++");
list.add("python");
list.add("c");
System.out.println("First element of the ArrayList: "+list.get(0));
System.out.println("Third element of the ArrayList: "+list.get(5));
	}
}

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on