Home > other >  Reverse subset of ArrayList using multidimensional ArrayList in Java
Reverse subset of ArrayList using multidimensional ArrayList in Java

Time:10-23

I am trying to reverse a sublist in a List using the indices provided in a multidimensional List. I don't have much experience using multidimensional lists/arrays. I don't understand why this doesn't work.

/*
   Given a List<Integer> list and List<List<Integer>> operations
   reverse the sublist and print out the list after all the operations have been done.
   Ex: [5, 3, 2, 1, 3]
       [[0,1], [1, 3]]
*/

import java.util.*;
public class ReverseParameters {
   public static void main(String[] args) {
       List<Integer> list = Arrays.asList(5, 3, 2, 1, 3);
       List<List<Integer>> operations = new ArrayList<>(2);
       for(int i= 0; i < 3; i  ){
           operations.add(new ArrayList<>());
       }
       operations.get(0).add(1);
       operations.get(1).add(3);

       subList(list, operations);
   }
   public static void subList (List<Integer> list, List<List<Integer>> operations) {
       System.out.println(list);
       int vertCount = operations.size();
       for (int i = 0; i < vertCount; i  ) {
           int edgeCount = operations.get(i).size();
           for (int j = 0; j < edgeCount; j  ) {
               int startInd = i;
               int endInd = operations.get(i).get(j);
               int shift = endInd - startInd;
               int right = Math.min(i   shift - 1, list.size() - 1);
               int temp = 0;
               while (startInd < right) {
                   temp = list.get(startInd);
                   list.set(startInd, list.get(right));
                   list.set(right, temp);
                   startInd =1;
                   right-=1;
               }
               System.out.println();
               System.out.printf(" %d %d%n", startInd, endInd);
               System.out.println();
           }
       }
       System.out.println(list);
   }
}

The output of this code using [[0,1], [1, 3]] as the indecies is:

[5, 2, 3, 1, 3]

but it should be:

[3, 1, 2, 5, 3]

Can someone please help point me in the right direction?

CodePudding user response:

You are overcomplicating your code with unnecessary variables that makes it difficult to find the problem. Please se a more simple code with explanation:

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(5, 3, 2, 1, 3);
    List<List<Integer>> operations = new ArrayList<>(2);
    // Initialize your operations
    operations.add(Arrays.asList(0,1));
    operations.add(Arrays.asList(1,3));
    subList(list, operations);
}
public static void subList (List<Integer> list, List<List<Integer>> operations) {
    // You just iterate over the operations
    for (List<Integer> operation : operations) {
        // For each operation, store left and right indexes.
        int left = operation.get(0);
        int right = operation.get(1);
        // Iterate until both indexes find each other
        while (left < right) {
            // Swap left and right elements in input list
            int aux = list.get(left);
            list.set(left, list.get(right));
            list.set(right, aux);

            // Now you move your indexes
              left;
            --right;
        }
    }
    System.out.println(list);
}

Please note that, depending on what the question asks, you may also need to verify if the operations indexes are within the list boundaries so you won't end up getting an ArrayIndexOutOfBoundsException. So be always careful with edge cases.

CodePudding user response:

This can be done as simple as this.

public class Main
{
    public static void main(String[] args)
    {
        //Given a List<Integer> list and List<List<Integer>> operations
        //reverse the sublist and print out the list after all the operations have been done.
        //Ex: [5, 3, 2, 1, 3] 
        //    [[0,1], [1, 3]]
        //Target: [3, 1, 2, 5, 3]
        
        //Steps:
        // 0  1  2  3  4 (Indices)
        //###############
        //[5, 3, 2, 1, 3]
        //[3, 5, 2, 1, 3] // Swap index of 0 and 1.
        //[3, 1, 2, 5, 3] // Swap index of 1 and 3.
        
        List<Integer> list = Arrays.asList(5, 3, 2, 1, 3);
        
        List<List<Integer>> listOfList = new ArrayList<List<Integer>>(2);
        listOfList.add(Arrays.asList(0, 1));
        listOfList.add(Arrays.asList(1, 3));
        
        for(int i=0; i < listOfList.size();   i) {
            final int indexA = listOfList.get(i).get(0); //[0 , [1
            final int indexB = listOfList.get(i).get(1); // 1],  3]
            
            //Swap the indices.
            final int tmpValue = list.get(indexA);
            list.set(indexA, list.get(indexB));
            list.set(indexB, tmpValue);
        }
        
        System.out.println(list);
        //[3, 1, 2, 5, 3]
    }
}

CodePudding user response:

You can get subset by List.subList() , and reverse by Collections.reverse().

static void reverseSubsets(List<Integer> list, List<List<Integer>> subsets) {
    for (List<Integer> subset : subsets)
        Collections.reverse(list.subList(subset.get(0), subset.get(1)   1));
}

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(5, 3, 2, 1, 3);
    List<List<Integer>> subsets = List.of(List.of(0, 1), List.of(1, 3));
    reverseSubsets(list, subsets);
    System.out.println(list);
}

output:

[3, 1, 2, 5, 3]

Note: toIndex in List.subList(int fromIndex, int toIndex) is exclusive high endpoint of the subList. So you must add 1 to subset.get(1).

  • Related