Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 *             Twidere - Twitter client for Android
 * 
 *  Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import android.support.annotation.NonNull;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static long[] intersection(final long[] array1, final long[] array2) {
        if (array1 == null || array2 == null)
            return new long[0];
        final List<Long> list1 = new ArrayList<>();
        for (final long item : array1) {
            list1.add(item);
        }
        final List<Long> list2 = new ArrayList<>();
        for (final long item : array2) {
            list2.add(item);
        }
        list1.retainAll(list2);
        return fromList(list1);
    }

    public static <T> T[] intersection(@NonNull final T[] array1, @NonNull final T[] array2) {
        final List<T> list1 = new ArrayList<>();
        Collections.addAll(list1, array1);
        final List<T> list2 = new ArrayList<>();
        Collections.addAll(list2, array2);
        list1.retainAll(list2);
        //noinspection unchecked
        return list1.toArray((T[]) Array.newInstance(array1.getClass().getComponentType(), list1.size()));
    }

    public static long[] fromList(final List<Long> list) {
        if (list == null)
            return null;
        final int count = list.size();
        final long[] array = new long[count];
        for (int i = 0; i < count; i++) {
            array[i] = list.get(i);
        }
        return array;
    }
}