Java examples for Collection Framework:List
Check element for existance for List
import java.util.ArrayList; public class Main { public static void main(String[] args) {//from w ww . ja va2s. c om // create a new ArrayList of Strings with an initial capacity of 10 ArrayList<String> items = new ArrayList<String>(); items.add("red"); // append an item to the list items.add(0, "yellow"); // insert "yellow" at index 0 // check if a value is in the List System.out.printf("\"red\" is %sin the list%n", items.contains("red") ? "": "not "); // display number of elements in the List System.out.printf("Size: %s%n", items.size()); items.remove(1); // remove item at index 1 display(items, "Remove second list element (green):"); // check if a value is in the List System.out.printf("\"red\" is %sin the list%n", items.contains("red") ? "": "not "); // display number of elements in the List System.out.printf("Size: %s%n", items.size()); } // display the ArrayList's elements on the console public static void display(ArrayList<String> items, String header) { System.out.print(header); // display header // display each element in items for (String item : items) System.out.printf(" %s", item); System.out.println(); } }