Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T[] removeAll(T[] a, T[] b, boolean usesEquals) {
        if (a.length == 0) {
            return a;
        } else {
            T[] result = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length);
            int i = 0;
            for (T t : a) {
                if (!exists(b, t, usesEquals)) {
                    result[i++] = t;
                }
            }
            if (i < result.length) {
                T[] res = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), i);
                System.arraycopy(result, 0, res, 0, i);
                return res;
            } else {
                return result;
            }
        }
    }

    /**
     * Generic method for checking if an item exists within an array.</br>
     * useEquals dictates to use '=' or '.equals()'.</br>
     * true represents '.equals'</br>
     * false represents '='
     * 
     * @param array
     * @param useEquals
     * @return
     */
    public static <T> boolean exists(T[] array, T item, boolean useEquals) {
        for (T t : array) {
            if ((useEquals && t.equals(item)) || (!useEquals && t == item)) {
                return true;
            }
        }
        return false;
    }
}