1

There is an Arraylist which has size 14 and I have to divide it into 6 sub-arraylists.

List<Integer> list;
list = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14));

For example like below :

14/6 = 2.333

Now when I'll take ceil value 3 then only 5 sub-arraylist will be created and when I'll take floor value i.e. 2 then 7 sub-arraylist will be created but I need 6.

How to achieve this ?

3
  • 2
    You can use subList(int fromIndex, int toIndex) Commented Nov 4, 2015 at 7:10
  • If you want exactly 6 sub list then some of your sublists must have different number of elements in it. Commented Nov 4, 2015 at 7:14
  • Output should come like this: [1,2,3] [4,5,6] [7,8] [9,10] [11,12] [13,14] Commented Nov 4, 2015 at 9:27

5 Answers 5

2

Something like this, might have some issues, but can be tweaked

 public static void main(String[] args) {
        List list = new ArrayList(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                11, 12, 13, 14));
        int number = 6;
        int factor = list.size() / number;

        int fromIndex = 0;
        int toIndex = factor;

        for (int i = 0; i < number; i++) {
            if (i < number - 1) {
                System.out.println(list.subList(fromIndex, toIndex));
                fromIndex = toIndex;
                toIndex = fromIndex + factor;
            } else {
                System.out.println(list.subList(fromIndex, list.size()));
            }
        }
    }

Input - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11, 12, 13, 14

Output

[1, 2]
[3, 4]
[5, 6]
[7, 8]
[9, 10]
[11, 12, 13, 14]

Input - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11, 12, 13

Output

[1, 2]
[3, 4]
[5, 6]
[7, 8]
[9, 10]
[11, 12, 13]
Sign up to request clarification or add additional context in comments.

1 Comment

Output should be like this: [1,2,3] [4,5,6] [7,8] [9,10] [11,12] [13,14]
0

I would suggest to use a "divide and conquer" method, use a recursive call to split the arrays in half until you get the number of arrays you need. Of course if you want an odd number of arrays, that would mean that you have one that is double the size of the rest.

Another solution would be to create 6 empty lists and just add an item to each while removing from the original until the original list is exhausted.

Comments

0

you can do something like below code

public static void main(String[] args) {
    List<String> arrayList = new ArrayList(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14));
    int noofsublist = 6;   //no of subparts
    int chopsize = Math.floor(arrayList.size()/noofsublist);

    for (int start = 0; start < arrayList.size(); start += chopsize) {
        int end = Math.min(start + chopsize, arrayList.size());
        List<String> sublist = arrayList.subList(start, end);
        System.out.println(sublist);
    }
}

Comments

0
List<String> list = new ArrayList(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14));
int parts = 6;

for (int i = 0; i < parts; i++) {
    List<String> part = list.subList(i * list.size() / parts,
                                     (i + 1) * list.size() / parts);
    System.out.println(part);
}

outputs

[1, 2]
[3, 4]
[5, 6, 7]
[8, 9]
[10, 11]
[12, 13, 14]

Comments

0

I guess this is a homework problem, but if it was a real-life scenario, I'd use Guava's Lists.partition() method:

List<List<Integer>> partitions = Lists.partition(yourList, n);

Where n is the maximum size of your sub list. A naive formula for n is yourList.size() / desiredNumberOfSubLists() + 1

In your case it would be 14 / 6 + 1 = 3, but unfortunately that creates 5 sub lists, not 6.

So here's a custom method that splits into n partitions, distributing the elements to the individual lists as fair as possible:

public static <T> List<List<T>> partitionIntoNLists(final List<T> list, final int n) {
    final List<List<T>> listOfLists = new ArrayList<>(n);
    final int[] partitionSizes = new int[n];
    int offset = 0;

    // round robin to distribute partition sizes
    for (int i = 0; i < list.size(); i++) {
        partitionSizes[offset++]++;
        if (offset == n) {
            offset = 0;
        }
    }

    offset = 0;
    for (final int partitionSize : partitionSizes) {
        listOfLists.add(list.subList(offset, offset + partitionSize));
        offset += partitionSize;
    }

    return listOfLists;
}

Test code:

public static void main(final String[] args) {
    final List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
    for (List<Integer> integers : partitionIntoNLists(list, 6)) {
        System.out.println(integers);
    }

}

Output:

[1, 2, 3]
[4, 5, 6]
[7, 8]
[9, 10]
[11, 12]
[13, 14]

4 Comments

If you partition 14 in partitions of 3, you will get 5 partitions, not 6.
@KenGeis true. There is no easy way to split into 6 Lists in a generic way
If by "easy" you mean a call to a popular library, then I'd agree. Please take a look at my answer.
@KenGeis your answer does not return the lists in the exact way the OP specified them (3 elements in the first lists only). My updated solution does

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.