Java ArrayList Remove removeAdditionalRanges(String line, ArrayList indices, ArrayList newRanges)

Here you can find the source of removeAdditionalRanges(String line, ArrayList indices, ArrayList newRanges)

Description

Removes ranges of characters from a line and adds their indices to an ArrayList.

License

BSD License

Parameter

Parameter Description
line the line to remove ranges of characters from
indices the existing ArrayList of indices
newRanges the int[2] ranges of indices to add, indexed without account for those already in the ArrayList

Return

line with the specified ranges removed

Declaration

private static String removeAdditionalRanges(String line, ArrayList<Integer> indices,
        ArrayList<int[]> newRanges) 

Method Source Code


//package com.java2s;
//License from project: BSD License 

import java.util.ArrayList;

public class Main {
    /**//ww w  .  j  av a  2s.  co m
     * Removes ranges of characters from a line and adds their indices to an ArrayList.
     * Assumes that the ranges are measured with the indices already ignored not being accounted for.
     * Expects all ranges to be int[2] where the first number is less than the second.
     * Expects the ranges to be sorted and not overlapping
     * 
     * @param line the line to remove ranges of characters from
     * @param indices the existing ArrayList of indices
     * @param newRanges the int[2] ranges of indices to add, indexed without account for those already in the ArrayList
     * @return line with the specified ranges removed
     */
    private static String removeAdditionalRanges(String line, ArrayList<Integer> indices,
            ArrayList<int[]> newRanges) {
        int offset = 0;
        for (int i = 0; i < newRanges.size(); i++) {
            line = line.substring(0, newRanges.get(i)[0]) + line.substring(newRanges.get(i)[1] + 1);
            while (indices.get(offset) <= newRanges.get(i)[0] + offset)
                offset++;
            int insideOffset = 0;
            while (offset + insideOffset < indices.size()
                    && indices.get(offset + insideOffset) <= newRanges.get(i)[1] + offset + insideOffset)
                insideOffset++;
            for (int j = offset; j < offset + insideOffset; j++)
                indices.remove(offset);
            for (int j = offset; j <= offset + insideOffset + newRanges.get(i)[1] - newRanges.get(i)[0]; j++)
                indices.add(j, newRanges.get(i)[0] + j);
            offset += insideOffset + newRanges.get(i)[1] - newRanges.get(i)[0];
            for (int j = i + 1; j < newRanges.size(); j++) {
                newRanges.get(j)[0] -= (newRanges.get(i)[1] - newRanges.get(i)[0]);
                newRanges.get(j)[1] -= (newRanges.get(i)[1] - newRanges.get(i)[0]);
            }
        }
        return line;
    }
}

Related

  1. removeAllFromArrayList(ArrayList collection, Collection toRemove)
  2. removeAllListaProd(ArrayList lista)
  3. removeAlreadyParsedFolders(ArrayList listStrings, String pickup, String limit)
  4. removeByReference(ArrayList list, T object)