Java program to group a sequential number from given numbers in a <list>

Suppose you are given unique numbers like

11,2,7,6,17,13,8,9,3,5,12

      

The result will be a list of number groups containing the ie submenu,

[2,3]-[5,6,7,8,9]-[11,12,13]-[17]

      

I took this approach to solve this issue below:

int[] a = { 11, 2, 7, 6, 13,17, 8, 9, 3, 5, 12 };

Arrays.sort(a);
List<List<Integer>> ListMain = new ArrayList<>();
List<Integer> temp = new ArrayList<>();

for (int i = 0; i < a.length; i++) {
    if (a[i + 1] == a[i] + 1) {
        temp.add(a[i + 1]);
    } else {
        ListMain.add(temp);
        temp.clear();
    }
}

      

+3


source to share


2 answers


Your general logic is mostly correct. However, you have a couple of problems when doing this.

  • You are getting an array index out of bounds exception because you are calling a[i+1]

    when i = a.length

    . Change the loop condition to a.length - 1

    .
  • Every time you need to use a new internal one ArrayList

    , otherwise each array will be destroyed. Change temp.clear();

    to temp = new ArrayList<>();

    .
  • You need to initialize a new list for the first element, not an empty list. Add temp.add(a[0]);

    at the beginning and temp.add(a[i+1])

    when you find you need a new sublist.
  • You need to add the final list at the end because your condition will not be called at the end of the loop.

Here's the modified program:



import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class SubList {
    public static void main(String... args) {
        int[] a = { 11, 2, 7, 6, 13,17, 8, 9, 3, 5, 12 };

        Arrays.sort(a);
        List<List<Integer>> ListMain = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        temp.add(a[0]);

        for (int i = 0; i < a.length - 1; i++) {
            if (a[i + 1] == a[i] + 1) {
                temp.add(a[i + 1]);
            } else {
                ListMain.add(temp);
                temp = new ArrayList<>();
                temp.add(a[i+1]);
            }
        }

        ListMain.add(temp);

        System.out.println(ListMain);
    }
}

      

Output:

[[2, 3], [5, 6, 7, 8, 9], [11, 12, 13], [17]]

      

+5


source


Thank you Garis M Suero for your suggestion, after which I received a response



    int[] a = { 11, 2, 7, 6, 13,17, 8, 9, 3, 5, 12 };

    Arrays.sort(a);

    List<List<Integer>> listMain = new ArrayList<List<Integer>>();
    List<Integer> temp = new ArrayList<>();

    for (int i = 0; i < a.length; i++) {
        if ((i + 1<a.length)&&( a[i] + 1==a[i + 1])) {
            temp.add(a[i]);
        } else {
            temp.add(a[i]);
            listMain.add(temp);
            temp  = new ArrayList<>();
        }

    }

    for (List<Integer> lstI : listMain) {
            for (Integer i : lstI) {
                System.out.println(i);
            }
        System.out.println("================");
     }

      

+2


source







All Articles