Quiz for java.util.List, contains method - Java Collection Framework

Java examples for Collection Framework:List

Introduction

  • Create instance of java.util.List
  • Add value to it with a for loop
  • Use for each loop to output values in the List.

Demo Code

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

public class Main {
  public static void main(String[] arguments) {
    List<String> list = new ArrayList<>();
    String[] codes = { "alpha", "lambda", "gamma", "delta", "zeta" };

    for (int i = 0; i < codes.length; i++) {
      if (!list.contains(codes[i])) {
        list.add(codes[i]);//ww  w .  j a  v  a 2 s. c  om
      }
    }
    // display all codes
    for (String code : list) {
      System.out.println(code);
    }
  }
}

Result


Related Tutorials