添加到元素中的索引数组

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<Integer> 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:[]

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home...
    光剑书架上的书阅读 9,452评论 2 8
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,064评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,954评论 18 399
  • 转载自:Java集合框架实例 1- 介绍 集合是程序和语言的基本思想。应用程序通常都会应用到集合,例如雇员的信息,...
    01_小小鱼_01阅读 3,180评论 0 1
  • 母校与我,在记忆的版图上,有一块永远的所在。那是青葱岁月的折射,是拔节长高的过程,是知识丝丝缕缕的吸取,是饥渴的双...
    花雨凤飞H阅读 3,012评论 10 12

友情链接更多精彩内容