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?

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. Commented Mar 25, 2021 at 5:11
  • One of the easiest and fastest way is : stackoverflow.com/questions/12099721/how-to-use-sublist Commented Mar 25, 2021 at 5: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}. Commented Mar 25, 2021 at 7:41

3 Answers 3

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())));
Sign up to request clarification or add additional context in comments.

Comments

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));
        }

2 Comments

At the first time in the loop, listOfLists.get (listIndex) throws an exception.
its because of not instantiated sub-lists. I edited the code
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);
    }

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.