List Class Quiz - Java Collection Framework

Java examples for Collection Framework:List

Introduction

  • Declare a List
  • Add String value to it if it is not yet being added.
  • Use Iterator to loop through the List

Demo Code


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

public class Main {

  public static void main(String[] arguments) {
    List<String> list;
    String[] codes = { "alpha", "lambda", "gamma", "delta", "zeta" };
    list = new ArrayList<>();
    for (int i = 0; i < codes.length; i++) {
      if (!list.contains(codes[i])) {
        list.add(codes[i]);//from   w w w . ja  v  a 2 s .  co  m
      }
    }
    for (Iterator<String> ite = list.iterator(); ite.hasNext();) {
      String output = (String) ite.next();
      System.out.println(output);
    }
  }
}

Result


Related Tutorials