How to Find Middle Element of a Given List in Java

In this program we will see that how to find the middle element of a given list in java. the list can be ArrayList, LinkedList or any other list implementation like vector etc.

here is a simple program to find the middle element of a given list in java

import java.util.ArrayList;
import java.util.List;

public class FindMiddle {
	
 public static void main(String[] args) {
	
	 List<String> list = new ArrayList<String>();
	 
	 for(int i=0; i<=50; i++){
		 list.add(String.valueOf(i));
	 }
	 
	 int middle = list.size()/2;
	 
	 String middleValue = list.get(middle);

	 System.out.println(middleValue);
	 
   }
}

In the above program we are adding elements to the list. Getting the size of the list and after obtaining the size dividing it by 2 so that we can get the middle value of the list. After getting the middle index we are using get method of List to get the middle value of the list. So that is a simple program to find middle element of a given list in java.

Leave a Comment