Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找Java中Int子数组的补充_Java_Arrays_Algorithm_Sorting_Set - Fatal编程技术网

查找Java中Int子数组的补充

查找Java中Int子数组的补充,java,arrays,algorithm,sorting,set,Java,Arrays,Algorithm,Sorting,Set,让我们有int[]A=new int[1000]和int[]subA=new int[300],这样subA\在中(subA是A的子集)。如何在Java中以最快的方式查找数组A\subA?对给定数组A和subA进行排序 编辑:对不起,忘了提到数组包含不同的元素,只是它们包含其他结构(如矩阵行)的索引 我正在考虑这个解决方案: // supp is short for supplement int[] supp = new int[A.length - subA.length]; int j =

让我们有
int[]A=new int[1000]
int[]subA=new int[300]
,这样
subA\在
中(
subA
A
的子集)。如何在Java中以最快的方式查找数组
A\subA
?对给定数组
A
subA
进行排序

编辑:对不起,忘了提到数组包含不同的元素,只是它们包含其他结构(如矩阵行)的索引

我正在考虑这个解决方案:

// supp is short for supplement
int[] supp = new int[A.length - subA.length];
int j = A[0], c = 0;
for (int i = 0; i < subA.lengh; i++) {
    // elegantly can be: while (j < subA[i]) supp[c++] = j++;
    while (j < subA[i]) {
        supp[c] = j;
        c++; j++;
    }
    j = subA[i] + 1;
}
//supp是supplement的缩写
int[]supp=新int[A.length-subA.length];
int j=A[0],c=0;
对于(int i=0;i

目前正在测试这种方法。答案准备好后我会回来的。

试试这样的方法:

// A index
int ai = 0;
// subA index
int sai = 0;
// result array
int[] result = new int[A.length - subA.length];
// index in result array
int resi = 0;

while ai < A.length && sai < subA.length;
    // same elements - ignore
    if (A[ai] == subA[sai]) {
        ai++;
        sai++;
    // found an element in A that does not exist in subA
    } else {
        // Store element
        result[resi] = A[ai];
        resi++;
        ai++;
    }
}

// Store elements that are left in A
for (;ai < A.length; ai++, resi++) {
    result[resi] = A[ai];
}
//索引
int ai=0;
//subA指数
int-sai=0;
//结果数组
int[]结果=新的int[A.length-subA.length];
//结果数组中的索引
int resi=0;
而ai
既然你说两个数组都是排序的,这听起来像是“我想让你遍历两个数组并从subA成员之间的数组中删除部分”这类作业

那么让我们试着把它草拟出来

  • 数组A按1000个成员排序
  • subA按300个成员排序
  • arrayA拥有subA的所有元素
意思是我们可以做一些像

public ArrayList findDifferences(int[] arrayA, int[] subA)
{
    ArrayList retVal = new ArrayList();
    for(int i = 0; i < arrayA.size; i++)
    {  
        if(arrayA[i] < subA[index]
            retVal.add(arrayA[i]);
        else if(arrayA[i] == subA[index])
            index++;
    }
    return retVal;
}

最快和最有效的方法是在上创建\SubA视图,即不保存自己对元素的引用,而是由和SubA支持。这类似于

当然,在创建该视图后,必须考虑对A和SubA的更改,这可能是优点,也可能是缺点,具体取决于您的情况

任意列表的示例性实现(即,在您的情况下,使用
新的ImmutableSubarrayList(Arrays.asList(A),Arrays.asList(SubA))

import java.util.AbstractSequentialList;
导入java.util.List;
导入java.util.ListIterator;
导入java.util.NoSuchElementException;
公共类ImmutableSubarrayList扩展了AbstractSequentialList{
最终清单a,subA;
最终整数大小;
公共ImmutableSubarrayList(列表aParam、列表SubParam){
超级();
a=阿帕拉姆;
subA=subparam;
断言a.containsAll(subA):“第二个列表只能包含第一个列表中的元素”;
//迭代,因为如果a包含相等的元素,则a.size()-subA.size()可能不正确。
int-sizeTemp=0;
对于(E元素:a){
如果(!子组件包含(元素)){
sizeTemp++;
}
}
尺寸=sizeTemp;
}
公共整数大小(){
返回大小;
}
公共ListIterator ListIterator(最终int firstIndex){
//创建一个并行的ListIterator
//迭代a和subA,仅返回a中不在subA中的元素
断言(firstIndex>=0&&firstIndex 0;
}
@凌驾
公共int nextIndex(){
返回下一个索引;
}
@凌驾
公共教育{
如果(!hasNext()){
抛出新的NoTouchElementException();
}
nextIndex++;
返回findNextElement();
}
@凌驾
公共服务{
如果(!hasPrevious()){
抛出新的NoTouchElementException();
}
下一个索引--;
返回findPreviousElement();
}
@凌驾
public int previousIndex(){
返回nextIndex-1;
}
@凌驾
公共无效集(E arg0){
抛出新的UnsupportedOperationException(“正在迭代的列表是不可变的”);
}
@凌驾
公共空间删除(){
抛出新的UnsupportedOperationException(“正在迭代的列表是不可变的”);
}
私有E findNextElement(){
E potentialNextElement=aIter.next();
while(subiter.hasNext()){
E nextElementToBeAvoided=子iter.next();
subiter.previous();
断言(潜在NextElement.compareTo(nextElementToBeAvoided)>0):
“要作废的NextElement不应小于潜在的NextElement”;
而(潜在NextElement.compareTo(nextElementToBeAvoided)=0){
potentialNextElement=aIter.next();
}
subiter.next();
}
返回电位;
}
//缺少lambdas:findNextElement()的克隆
私有E findPreviousElement(){
E potentialPreviousElement=aIter.previous();
while(subiter.hasPrevious()){
E previousElementToBeAvoided=子iter.previous();
subiter.previous();
断言(潜在的PreviousElement.compareTo(previousElementToBeAvoided)<0):
“要作废的PreviousElement不应大于潜在PreviousElement”;
while(潜在的PreviousElement.compareTo(previousElementToBeAvoided)==0){
潜在先前
 List a = new List();
 a.addAll(arrayA);
 List b = new List();
 b.addAll(subA);
 a.removeAll(b);
 return a;
import java.util.AbstractSequentialList;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;


public class ImmutableSubarrayList<E extends Comparable<E>> extends AbstractSequentialList<E>{

    final List<E> a, subA;
    final int size;

    public ImmutableSubarrayList(List<E> aParam, List<E> subAParam){
        super();
        a = aParam;
        subA = subAParam;
        assert a.containsAll(subA) : "second list may only contain elements from first list";

        // Iterate over a, because a.size()-subA.size() may not be correct if a contains equal elements. 
        int sizeTemp = 0;
        for (E element : a){    
            if (!subA.contains(element)){
                sizeTemp++;
            }
        }
        size = sizeTemp;
    }

    public int size() {
        return size;
    }

    public ListIterator<E> listIterator(final int firstIndex) {
        //create a ListIterator that parallely 
        // iterates over a and subA, only returning the elements in a that are not in subA
        assert (firstIndex >=0 && firstIndex <= ImmutableSubarrayList.this.size()) : "parameter was "
                           +firstIndex+" but should be betwen 0 and "+ImmutableSubarrayList.this.size();
        return new ListIterator<E>() {

            private final ListIterator<E> aIter = a.listIterator();
            private final ListIterator<E> subAIter = subA.listIterator();
            private int nextIndex = 0;

            {
                for (int lv = 0; lv < firstIndex; lv++ ){
                    next();
                }
            }

            @Override
            public boolean hasNext() {
                return nextIndex < size;
            }

            @Override
            public void add(E arg0) {
                throw new UnsupportedOperationException("The list being iteratred over is immutable");
            }

            @Override
            public boolean hasPrevious() {
                return nextIndex > 0;
            }

            @Override
            public int nextIndex() {
                return nextIndex;
            }

            @Override
            public E next() {
                if (!hasNext()){
                    throw new NoSuchElementException();
                }
                nextIndex++;
                return findNextElement();
            }

            @Override
            public E previous() {
                if (!hasPrevious()){
                    throw new NoSuchElementException();
                }
                nextIndex--;
                return findPreviousElement();
            }

            @Override
            public int previousIndex() {
                return nextIndex-1;
            }

            @Override
            public void set(E arg0) {
                throw new UnsupportedOperationException("The list being iteratred over is immutable");
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("The list being iteratred over is immutable");          
            }

            private E findNextElement() {
                E potentialNextElement = aIter.next();
                while (subAIter.hasNext()){
                    E nextElementToBeAvoided = subAIter.next();
                    subAIter.previous();
                    assert (potentialNextElement.compareTo(nextElementToBeAvoided) > 0) : 
                        "nextElementToBeAvoided should not be smaller than potentialNextElement";
                    while (potentialNextElement.compareTo(nextElementToBeAvoided) == 0){
                        potentialNextElement = aIter.next();
                    }
                    subAIter.next();
                }
                return potentialNextElement;
            }

            //in lack of lambdas: clone of findNextElement()
            private E findPreviousElement() {
                E potentialPreviousElement = aIter.previous();
                while (subAIter.hasPrevious()){
                    E previousElementToBeAvoided = subAIter.previous();
                    subAIter.previous();
                    assert (potentialPreviousElement.compareTo(previousElementToBeAvoided) < 0) : 
                        "previousElementToBeAvoided should not be greater than potentialPreviousElement";
                    while (potentialPreviousElement.compareTo(previousElementToBeAvoided) == 0){
                        potentialPreviousElement = aIter.previous();
                    }
                    subAIter.previous();
                }
                return potentialPreviousElement;
            }
        };
    }
}
    int index = Arrays.binarySearch(A, subA[0]);

    int[] diff = new int[A.length - subA.length];

    System.arraycopy(A, 0, diff, 0, index);
    System.arraycopy(A, index+subA.length, diff, index, A.length-index-subA.length);