Online Java Compiler By
JavaTpoint.com
import java.util.*; public class CollectionsCheckedListExample2 { public static void main(String[] args) { List myList = new ArrayList(); myList.add("one"); myList.add("two"); myList.add("three"); myList.add("four"); List chkList = Collections.checkedList(myList, String.class); System.out.println("Checked list content: "+chkList); //you can add any type of elements to myList object myList.add(10); //you cannot add any type of elements to chkList object, doing so throws ClassCastException chkList.add(10); //throws ClassCastException } }
Output