简体   繁体   English

用其他ArrayList替换ArrayList的一部分

[英]Replace part of ArrayList with other ArrayList

Is there any simple way (for example library function) to replace fragment of one ArrayList with another ArrayList? 是否有任何简单的方法(例如库函数)将一个ArrayList的片段替换为另一个ArrayList? What I want to do is: 我想做的是:

 ArrayList<Byte> fileArr = // some bytes //
 ArrayList<Byte> toReplace = // some bytes I want to replace in fileArray //
 ArrayList<Byte> window = // window with bytes from file and size of toReplace List  //
 ArrayList<Byte> replacing = // bytes I want to replace with //
 for(int i = 0; i <= (maxElementOfFileArray - sizeOfToReplace); i++){
 window = fileArr.subList(i, i+sizeOfToReplace) 
 if(window.equals(toReplace){
      fileArr.replaceAll(i, toReplace, replacing) 
 }
i= indexOfReplacingListFinishInFileList -1; 
}

where the replaceAll function would replace elements of file ArrayList from index where subList toReplace occurs with elements of replacing list, and here's the catch: toReplace and replacing may be diffrent size Lists. 其中replaceAll函数将从索引中替换文件ArrayList的元素,其中subList toReplace出现在替换列表元素中,这是要抓住的地方:toReplace和替换可能是大小不同的Lists。 Because if they would be the same size I just would do that with set function of ArraList in "for(E e : elements)" . 因为如果它们的大小相同,我只需要使用“ for(E e:elements)”中的ArraList的set函数来实现。 So replace function can change size of file ArrayList it's changing. 因此,replace函数可以更改正在更改的ArrayList文件的大小。

You could try 你可以试试

fileArr.removeRange(i, i+sizeOfToReplace);
fileArr.addAll(i, replacing);

You can clear, then add the new elements to the window. 您可以清除,然后将新元素添加到窗口中。

By the ways you can use Collections.indexOfSubList to find the window position. 通过方法,可以使用Collections.indexOfSubList查找窗口位置。

    List<Byte> fileArr = new ArrayList(Arrays.asList(new Byte[]{1, 2, 3, 4, 5, 6}));
    List<Byte> toReplace = Arrays.asList(new Byte[]{3, 4});
    List<Byte> replacing = Arrays.asList(new Byte[]{13, 14, 15, 16, 17, 18});

    int idx = Collections.indexOfSubList(fileArr, toReplace);

    if (idx >= 0) {
        List<Byte> window = fileArr.subList(idx, idx + toReplace.size());
        window.clear();
        window.addAll(replacing);
    }

    System.out.println(fileArr);


    [1, 2, 13, 14, 15, 16, 17, 18, 5, 6]

Write a utility method to accept a generic array and replace a given subarray, if found, with all the possible checks. 编写一个实用程序方法来接受通用数组,并在找到所有可能的检查后替换给定的子数组。

You can test for the presence of the subarray: 您可以测试是否存在子数组:

    int firstIndex = Collections.indexOfSubList(source, target);

    if (firstIndex == -1 ){
        // the sublist doesn't exist
    }

    int lastIndex = Collections.lastIndexOfSubList(source, target);

From there you have your insertion points and can use a library to insert or write a brute force method. 从那里可以找到插入点,并且可以使用库来插入或编写蛮力方法。 For instance you can get the sublist from the List interface and make the changes to that sublist. 例如,您可以从“列表”界面获取子列表,然后对该子列表进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM