Skip to content

Instantly share code, notes, and snippets.

@gauravkukade
Created December 24, 2021 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauravkukade/36b1aaf03231e643d70d97e6a0f1c9d5 to your computer and use it in GitHub Desktop.
Save gauravkukade/36b1aaf03231e643d70d97e6a0f1c9d5 to your computer and use it in GitHub Desktop.
A java program to get the element by its index using the get() method. Read more at https://coderolls.com/arraylist-get-method/
import java.util.ArrayList;
import java.util.List;
/**
* A java program to get the element by
* its index using the get() method
*
* @author Gaurav Kukade at coderolls.com
*/
public class ArrayListGetExample {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Monday");
list.add("Tuesday");
list.add("Wednesday");
list.add("Thursday");
list.add("Friday");
list.add("Saturday");
list.add("Sunday");
System.out.println("Arraylist:"+ list);
// get element at index 3
String element = list.get(3);
System.out.println("\nElement at index 3 is "+ element);
System.out.println("\nUsing get() method in for loop: \n");
// using the get(0 method in for loop
for(int i=0; i<list.size(); i++) {
System.out.println("The element at index "+i+" is "+ list.get(i));
}
}
}
Arraylist:[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]
Element at index 3 is Thursday
Using get() method in for loop:
The element at index 0 is Monday
The element at index 1 is Tuesday
The element at index 2 is Wednesday
The element at index 3 is Thursday
The element at index 4 is Friday
The element at index 5 is Saturday
The element at index 6 is Sunday
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment