Search code examples
javaarraysarraylistsplitedges

Splitting arraylist into seperate arrays with every 3rd element being the 1st element of the next


I am trying to split arraylists into seperate arrays in order to obtain "edges". I need to split the arraylist into arrays of size 3 with the 3rd element of the newly split-created array being the 1st element of the next. What i have currently done is that the arraylist is successfully split into arrays of 3 although the 3rd element is not the 1st element of the next.

Example: [1,2,3,4,5,6,7] would need to be split up as [1,2,3],[3,4,5],[5,6,7] but what i am getting is [1,2,3],[4,5,6],[7]

This is the code i used:

for(ArrayList<String> path : allPaths) {
    edges = separate(path, 3);
}
for(List<String> edge : edges){
    System.out.println("Edge: "+edge);
}

Then this method is accessed

static <T> List<List<T>> separate (List<T> path, final int size){
    List<List<T>> separated = new ArrayList<>();

    for(int i = 0; i < path.size(); i+= size){
        separated.add(new ArrayList<>(path.subList(i, Math.min(path.size(), i + size))));
    }
    return separated;
}

Solution

  • Do it as follows:

    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
        public static void main(String[] args) {
            List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
            System.out.println(separate(list, 3));
        }
    
        static <T> List<List<T>> separate(List<T> path, final int size) {
            List<List<T>> separated = new ArrayList<>();
            List<T> temp;
            for (int i = 0; i < path.size(); i += size - 1) {
                temp = (List<T>) path.subList(i, Math.min(path.size(), i + size));
                if (temp.size() != 1) {
                    separated.add(new ArrayList<T>(temp));
                }
            }
            return separated;
        }
    }
    

    Output:

    [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]]
    

    Update

    @Mr Mosby - You posted the comment about the additional requirement of not adding the sublist to separated if it has just one element and accordingly I updated my answer given above. Just in case your requirement is, not to add the sublist to separated when it has less number of elements than size, given below is the solution:

    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
        public static void main(String[] args) {
            List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8);
            System.out.println(separate(list, 3));
        }
    
        static <T> List<List<T>> separate(List<T> path, final int size) {
            List<List<T>> separated = new ArrayList<>();
            List<T> temp;
            for (int i = 0; i < path.size(); i += size - 1) {
                temp = (List<T>) path.subList(i, Math.min(path.size(), i + size));
                if (temp.size() == size) {
                    separated.add(new ArrayList<T>(temp));
                }
            }
            return separated;
        }
    }
    

    Output:

    [[1, 2, 3], [3, 4, 5], [5, 6, 7]]