3

Is there any simple way (for example library function) to replace fragment of one ArrayList with another 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. Because if they would be the same size I just would do that with set function of ArraList in "for(E e : elements)" . So replace function can change size of file ArrayList it's changing.

3 Answers 3

4

You could try

fileArr.removeRange(i, i+sizeOfToReplace);
fileArr.addAll(i, replacing);
Sign up to request clarification or add additional context in comments.

2 Comments

Checking the code of ArrayList, this mostly does the work using System.arraycopy, so should not be terribly slow. There is one loop nulling the data on the end of the array, so if you're doing this a lot, consider testing if a custom method is more efficient.
Yet, removeRange is protected AbstractList function, so I was forced to call it indirectly with "fileArr.subList(i, i + sizeOfToReplace).clear()". Thank you very much.
2

You can clear, then add the new elements to the window.

By the ways you can use Collections.indexOfSubList to find the window position.

    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]

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.