Here you can find the source of intersect(T[] a, T[] b)
Parameter | Description |
---|---|
a | a sorted array |
b | a sorted array of similar size to <code>a</code> |
T | element type |
public static <T extends Comparable<T>> Object[] intersect(T[] a, T[] b)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; public class Main { /**/*from www.j a v a2 s.c o m*/ * Finds the intersection of two sorted arrays. * * @param a a sorted array * @param b a sorted array of similar size to <code>a</code> * @param <T> element type * @return the intersection of two sorted arrays */ public static <T extends Comparable<T>> Object[] intersect(T[] a, T[] b) { if (a.length == 0 || b.length == 0) return new Object[0]; List<T> ret = new ArrayList<>(); int i = 0; for (T val : a) { while (i < b.length && val.compareTo(b[i]) > 0) { ++i; } if (i < b.length && val.compareTo(b[i]) == 0) { ret.add(val); ++i; } } return ret.toArray(); } }