Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;

public class Main {

    public static <T> ArrayList<T> intersectionList(ArrayList<T> arg1, ArrayList<T> arg2) {
        ArrayList<T> intersection = null;
        if (null != arg1 && null != arg2) {
            intersection = new ArrayList<T>();
            for (T t1 : arg1) {
                for (T t2 : arg2) {
                    if (t1.equals(t2)) {
                        intersection.add(t1);
                    }
                }
            }
        }
        return distinctList(intersection);
    }

    public static <T> ArrayList<T> distinctList(ArrayList<T> arg) {
        ArrayList<T> distinct = new ArrayList<T>();
        if (null != arg) {
            distinct = new ArrayList<T>();
            distinct.addAll(arg);
            for (int i = 0; i < distinct.size(); i++) {
                T t1 = distinct.get(i);
                for (int j = i + 1; j < distinct.size(); j++) {
                    T t2 = distinct.get(j);
                    if (t1.equals(t2)) {
                        distinct.remove(j);
                        j--;
                    }
                }
            }
        }
        return distinct;
    }
}