添加到元素中的索引数组

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class ComputesTheArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Collection smallCollection = java.util.Arrays.asList("asdf",
            "java");
    Collection bigCollection = java.util.Arrays.asList("asdf",
            "java");
    System.out.println(java.util.Arrays
            .toString(computeDifferenceIndices(smallCollection,
                    bigCollection)));
}
/**
 * Computes the array of element indices which where added to a collection.
 *
 * @param smallCollection
 *          the original collection.
 * @param bigCollection
 *          the collection with added elements.
 * @return the the array of element indices which where added to the original
 *         collection
 */
public static int[] computeDifferenceIndices(
        Collection smallCollection, Collection bigCollection) {
    List addedIndices = new ArrayList<>();
    int index = 0;
    for (Iterator ite = bigCollection.iterator(); ite.hasNext(); index++) {
        if (smallCollection == null
                || !smallCollection.contains(ite.next())) {
            if (smallCollection == null) {
                ite.next();
            }
            addedIndices.add(index);
        }
    }
    int[] result = new int[addedIndices.size()];
    for (int i = 0; i < addedIndices.size(); i++) {
        result[i] = addedIndices.get(i);
    }
    return result;
}

}
Console:[]

你可能感兴趣的:(添加到元素中的索引数组)