Sorted List Merge/Remove via For Loop - Java Algorithm

Java examples for Algorithm:List

Description

Sorted List Merge/Remove via For Loop

Demo Code


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

public class Main {

  public static void main(String args[]) {

    int[] a1 = { 1, 3, 8, 34, 65, 71 };
    int[] a2 = { 1, 11, 34, 71, 90 };

    List set1 = new ArrayList();
    List set2 = new ArrayList();

    for (int i = 0; i < a1.length; i++) {
      for (int j = 0; j < a2.length; j++) {
        if (a1[i] == a2[j])
          set1.add(a1[i]);//from   w  ww.j  a v a  2 s  .c  o  m
      }
      if (!set1.contains(a1[i]))
        set2.add(a1[i]);
    }
    System.out.println(set1);
    System.out.println(set2);

    for (int i = 0; i < a2.length; i++) {
      if (!set1.contains(a2[i]))
        set2.add(a2[i]);
    }
    System.out.println(set2);

  }
}

Related Tutorials