ArrayList Collections Add new Item Sort Find Item Java example

How add item to ArrayList, sort ArrayList, search find index of item in ArrayList, min(), max() Java basic example.

import java.util.ArrayList;
import java.util.Collections;
 
public class MainClass {
	public static void main(String[] arg) {
		String[] arrayOfString = {"nothing", "Hello", "people"
				, "bye-bye", "hello", "world!", "End" };
		   ArrayList<String> arrayList = new ArrayList<String>();
            for(String s: arrayOfString)
		     arrayList.add(s);
 
		    Collections.sort(arrayList);
		    // foreach
		    for (String str: arrayList)
		      System.out.println(str);
 
		    Object objMin = Collections.min(arrayList);
		    System.out.println("Min is: " + objMin);
 
		    Object objMax = Collections.max(arrayList);
		    System.out.println("Max is: " + objMax);
 
		    int index = Collections.binarySearch(arrayList, "people");
		    System.out.println("Index of people is: " + index);
 
		  }
 
 
 
}
/*
End
Hello
bye-bye
hello
nothing
people
world!
Min is: End
Max is: world!
Index of people is: 5
*/
This entry was posted in Android Examples Code, Array,List,Collections and tagged , , , , , . Bookmark the permalink.


Comments are closed.



9,582 views