1

I am trying to split the Arraylist into numbers of Arraylists with the rule:

  • Split the arraylist into several sub-arraylists when the value of index i in arraylist is bigger than the value of index i+1.

For example, if there is an arraylist {6,7,8,3,9,10,1,4}, it should be stored as { {6,7,8}, {3,9,10}, {1,4} }.

How can I do this one?

dominono
  • 25
  • 3
  • any index `i` is by definition less than `i+1` (it is, in fact, one less), so you're going to have be more precise in your description. Did you mean the _element_ at index `i` and `i+1`? Also, "it should be stored in another arraylist" suggests that in "6,7,8,3,9", only 8 should be put in its own list, so again: you need edit your question to be a lot more precise. – Mike 'Pomax' Kamermans Mar 25 '21 at 05:11
  • One of the easiest and fastest way is : https://stackoverflow.com/questions/12099721/how-to-use-sublist – Stanislav Zaiac Mar 25 '21 at 05:50
  • As I understand your requirements, each sub-list must contain numbers in descending order only. From your sample input, you should only have two sub-lists as follows: `{8,3}` and `{10, 1}`. – Abra Mar 25 '21 at 07:41

3 Answers3

2

By looping through the original arraylist and using the sublist method the below code should solve your problem.

    ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(6,7,8,3,9,10,1,4));

    int indexToSplitFrom = 0;
    for(int i = 0; i < numbers.size() - 1; i++){
        System.out.println(numbers.get(i));
        if(numbers.get(i) > numbers.get(i+1)){
            lists.add(new ArrayList<>(numbers.subList(indexToSplitFrom, i + 1)));
            indexToSplitFrom = i + 1;
        }
    }
    lists.add(new ArrayList<>(numbers.subList(indexToSplitFrom,  + numbers.size())));
Ctrain
  • 36
  • 1
  • 4
1

This code should work for you

        List<Integer> mainList = new ArrayList<>();
        List<List<Integer>> listOfLists = new ArrayList<>();
        int listIndex = 0;
        for (int i = 0; i < mainList.size(); i++) {
            if (i == 0) {
                listOfLists.add(new ArrayList<>());
            } else if (mainList.get(i) < mainList.get(i - 1)) {
                listOfLists.add(new ArrayList<>());
                listIndex++;
            }
            listOfLists.get(listIndex).add(mainList.get(i));
        }
1
public static void splitList(ArrayList<Integer> input) {
        ArrayList<ArrayList<Integer>> output = new ArrayList<ArrayList<Integer>>();
        System.out.println("Input : "+input);
        for (int count = 0; count < input.size(); count++) {

            ArrayList<Integer> subList = new ArrayList<Integer>();
            for (int index = count; index < input.size(); index++) {
                if((index == input.size() - 1) || (input.get(index) > input.get(index + 1))){
                    subList.add(input.get(index));
                    count = index;
                    break;
                }else {
                    subList.add(input.get(index));
                }
            }
            output.add(subList);
        }
        System.out.println("Output : "+output);
    }
Veera
  • 41
  • 4