How to split an array into multi arrays in java? [duplicate]
This question already has an answer here:
How to split a string array into small chunk arrays in java?
9 answers
I have an array
[2,5,7,9,1,3,4,6,8]
and I want a function
List<List<Integer>> split(List<Integer> list, int n)
which will separate the list intonarrays
for example:n=4,
result will be:
[2,1,8],
[5,3],
[7,4],
[9,6]
Note, the number was sampled averagely
java
marked as duplicate by Raedwald, Paul, MC Emperor, Mark Rotteveel
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 16:49
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to split a string array into small chunk arrays in java?
9 answers
I have an array
[2,5,7,9,1,3,4,6,8]
and I want a function
List<List<Integer>> split(List<Integer> list, int n)
which will separate the list intonarrays
for example:n=4,
result will be:
[2,1,8],
[5,3],
[7,4],
[9,6]
Note, the number was sampled averagely
java
marked as duplicate by Raedwald, Paul, MC Emperor, Mark Rotteveel
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 16:49
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Do you want a random separation?
– Alon
Nov 13 '18 at 9:25
1
@Turamarth the order is not same
– Jimmy Guo
Nov 13 '18 at 9:25
1
@Alon sample averagely
– Jimmy Guo
Nov 13 '18 at 9:25
2
What have you tried so far? I don't see any problems here. Implementingsplitis rather straight forward.
– Socowi
Nov 13 '18 at 9:27
@Turamarth The input in that case appears to specifying the size of the chunks, where in this case it would be the number of chunks.
– Michael
Nov 13 '18 at 9:56
add a comment |
This question already has an answer here:
How to split a string array into small chunk arrays in java?
9 answers
I have an array
[2,5,7,9,1,3,4,6,8]
and I want a function
List<List<Integer>> split(List<Integer> list, int n)
which will separate the list intonarrays
for example:n=4,
result will be:
[2,1,8],
[5,3],
[7,4],
[9,6]
Note, the number was sampled averagely
java
This question already has an answer here:
How to split a string array into small chunk arrays in java?
9 answers
I have an array
[2,5,7,9,1,3,4,6,8]
and I want a function
List<List<Integer>> split(List<Integer> list, int n)
which will separate the list intonarrays
for example:n=4,
result will be:
[2,1,8],
[5,3],
[7,4],
[9,6]
Note, the number was sampled averagely
This question already has an answer here:
How to split a string array into small chunk arrays in java?
9 answers
java
java
edited Nov 13 '18 at 9:26
Jimmy Guo
asked Nov 13 '18 at 9:22
Jimmy GuoJimmy Guo
548414
548414
marked as duplicate by Raedwald, Paul, MC Emperor, Mark Rotteveel
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 16:49
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Raedwald, Paul, MC Emperor, Mark Rotteveel
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 16:49
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Do you want a random separation?
– Alon
Nov 13 '18 at 9:25
1
@Turamarth the order is not same
– Jimmy Guo
Nov 13 '18 at 9:25
1
@Alon sample averagely
– Jimmy Guo
Nov 13 '18 at 9:25
2
What have you tried so far? I don't see any problems here. Implementingsplitis rather straight forward.
– Socowi
Nov 13 '18 at 9:27
@Turamarth The input in that case appears to specifying the size of the chunks, where in this case it would be the number of chunks.
– Michael
Nov 13 '18 at 9:56
add a comment |
1
Do you want a random separation?
– Alon
Nov 13 '18 at 9:25
1
@Turamarth the order is not same
– Jimmy Guo
Nov 13 '18 at 9:25
1
@Alon sample averagely
– Jimmy Guo
Nov 13 '18 at 9:25
2
What have you tried so far? I don't see any problems here. Implementingsplitis rather straight forward.
– Socowi
Nov 13 '18 at 9:27
@Turamarth The input in that case appears to specifying the size of the chunks, where in this case it would be the number of chunks.
– Michael
Nov 13 '18 at 9:56
1
1
Do you want a random separation?
– Alon
Nov 13 '18 at 9:25
Do you want a random separation?
– Alon
Nov 13 '18 at 9:25
1
1
@Turamarth the order is not same
– Jimmy Guo
Nov 13 '18 at 9:25
@Turamarth the order is not same
– Jimmy Guo
Nov 13 '18 at 9:25
1
1
@Alon sample averagely
– Jimmy Guo
Nov 13 '18 at 9:25
@Alon sample averagely
– Jimmy Guo
Nov 13 '18 at 9:25
2
2
What have you tried so far? I don't see any problems here. Implementing
split is rather straight forward.– Socowi
Nov 13 '18 at 9:27
What have you tried so far? I don't see any problems here. Implementing
split is rather straight forward.– Socowi
Nov 13 '18 at 9:27
@Turamarth The input in that case appears to specifying the size of the chunks, where in this case it would be the number of chunks.
– Michael
Nov 13 '18 at 9:56
@Turamarth The input in that case appears to specifying the size of the chunks, where in this case it would be the number of chunks.
– Michael
Nov 13 '18 at 9:56
add a comment |
4 Answers
4
active
oldest
votes
Try this:
public static ArrayList<ArrayList<Integer>> split(ArrayList<Integer> list, int n)
ArrayList<ArrayList<Integer>> resultArrays = new ArrayList<>();
int pivot = 0;
// Creates n ArrayLists.
for(int i = 0 ; i < n ; i++)
resultArrays.add(new ArrayList<>());
// Add element from list to new ArrayLists.
while(pivot != list.size())
int p = pivot%n;
resultArrays.get(p).add(list.get(pivot));
pivot++;
return resultArrays;
The result is the one you exactly you want.
[2,1,8],
[5,3],
[7,4],
[9,6]
pivot points list's element.(from 0 to the size-1)
p points the ArrayList of ArrayList's index.(As big as n, here 4)
So,
num / *(pivot) / ArrayList[p]
1: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
2: [2,5,7,9,1,3,4,6,8] -> ArrayList[1]
*
3: [2,5,7,9,1,3,4,6,8] -> ArrayList[2]
*
4: [2,5,7,9,1,3,4,6,8] -> ArrayList[3]
*
5: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
6: [2,5,7,9,1,3,4,6,8] -> ArrayList[1]
*
7: [2,5,7,9,1,3,4,6,8] -> ArrayList[2]
*
8: [2,5,7,9,1,3,4,6,8] -> ArrayList[3]
*
9: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
add a comment |
Given a number of bins.
List<List<Integer>> result = new ArrayList<>();
while(result.size()<bins)
result.add(new ArrayList<>());
int counter = 0;
for(Integer i: input)
result.get(counter++%bins).add(i);
add a comment |
In Guava library there is a List.Partition method.
List<List<Integer>> split(List<Integer> list, int n)
return Lists.partition(list, n);
This is not what OP wants. Have a close look at the expected output. The order is different.
– Socowi
Nov 13 '18 at 9:28
add a comment |
List<List<Integer>> split(List<Integer> list, int n)
List<List<Integer>> result = new ArrayList<>();
//add n ArrayLists to the result list
IntStream.range(0, n).forEach(k->result.add(new ArrayList<>()));
//iterate over the input list and add one element to one of the inner list of the result array
Iterator i = list.iterator();
while(i.hasNext())
int count = 0;
while(i.hasNext() && count < n)
result.get(count).add((Integer) i.next());
count++;
return result;
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
public static ArrayList<ArrayList<Integer>> split(ArrayList<Integer> list, int n)
ArrayList<ArrayList<Integer>> resultArrays = new ArrayList<>();
int pivot = 0;
// Creates n ArrayLists.
for(int i = 0 ; i < n ; i++)
resultArrays.add(new ArrayList<>());
// Add element from list to new ArrayLists.
while(pivot != list.size())
int p = pivot%n;
resultArrays.get(p).add(list.get(pivot));
pivot++;
return resultArrays;
The result is the one you exactly you want.
[2,1,8],
[5,3],
[7,4],
[9,6]
pivot points list's element.(from 0 to the size-1)
p points the ArrayList of ArrayList's index.(As big as n, here 4)
So,
num / *(pivot) / ArrayList[p]
1: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
2: [2,5,7,9,1,3,4,6,8] -> ArrayList[1]
*
3: [2,5,7,9,1,3,4,6,8] -> ArrayList[2]
*
4: [2,5,7,9,1,3,4,6,8] -> ArrayList[3]
*
5: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
6: [2,5,7,9,1,3,4,6,8] -> ArrayList[1]
*
7: [2,5,7,9,1,3,4,6,8] -> ArrayList[2]
*
8: [2,5,7,9,1,3,4,6,8] -> ArrayList[3]
*
9: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
add a comment |
Try this:
public static ArrayList<ArrayList<Integer>> split(ArrayList<Integer> list, int n)
ArrayList<ArrayList<Integer>> resultArrays = new ArrayList<>();
int pivot = 0;
// Creates n ArrayLists.
for(int i = 0 ; i < n ; i++)
resultArrays.add(new ArrayList<>());
// Add element from list to new ArrayLists.
while(pivot != list.size())
int p = pivot%n;
resultArrays.get(p).add(list.get(pivot));
pivot++;
return resultArrays;
The result is the one you exactly you want.
[2,1,8],
[5,3],
[7,4],
[9,6]
pivot points list's element.(from 0 to the size-1)
p points the ArrayList of ArrayList's index.(As big as n, here 4)
So,
num / *(pivot) / ArrayList[p]
1: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
2: [2,5,7,9,1,3,4,6,8] -> ArrayList[1]
*
3: [2,5,7,9,1,3,4,6,8] -> ArrayList[2]
*
4: [2,5,7,9,1,3,4,6,8] -> ArrayList[3]
*
5: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
6: [2,5,7,9,1,3,4,6,8] -> ArrayList[1]
*
7: [2,5,7,9,1,3,4,6,8] -> ArrayList[2]
*
8: [2,5,7,9,1,3,4,6,8] -> ArrayList[3]
*
9: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
add a comment |
Try this:
public static ArrayList<ArrayList<Integer>> split(ArrayList<Integer> list, int n)
ArrayList<ArrayList<Integer>> resultArrays = new ArrayList<>();
int pivot = 0;
// Creates n ArrayLists.
for(int i = 0 ; i < n ; i++)
resultArrays.add(new ArrayList<>());
// Add element from list to new ArrayLists.
while(pivot != list.size())
int p = pivot%n;
resultArrays.get(p).add(list.get(pivot));
pivot++;
return resultArrays;
The result is the one you exactly you want.
[2,1,8],
[5,3],
[7,4],
[9,6]
pivot points list's element.(from 0 to the size-1)
p points the ArrayList of ArrayList's index.(As big as n, here 4)
So,
num / *(pivot) / ArrayList[p]
1: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
2: [2,5,7,9,1,3,4,6,8] -> ArrayList[1]
*
3: [2,5,7,9,1,3,4,6,8] -> ArrayList[2]
*
4: [2,5,7,9,1,3,4,6,8] -> ArrayList[3]
*
5: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
6: [2,5,7,9,1,3,4,6,8] -> ArrayList[1]
*
7: [2,5,7,9,1,3,4,6,8] -> ArrayList[2]
*
8: [2,5,7,9,1,3,4,6,8] -> ArrayList[3]
*
9: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
Try this:
public static ArrayList<ArrayList<Integer>> split(ArrayList<Integer> list, int n)
ArrayList<ArrayList<Integer>> resultArrays = new ArrayList<>();
int pivot = 0;
// Creates n ArrayLists.
for(int i = 0 ; i < n ; i++)
resultArrays.add(new ArrayList<>());
// Add element from list to new ArrayLists.
while(pivot != list.size())
int p = pivot%n;
resultArrays.get(p).add(list.get(pivot));
pivot++;
return resultArrays;
The result is the one you exactly you want.
[2,1,8],
[5,3],
[7,4],
[9,6]
pivot points list's element.(from 0 to the size-1)
p points the ArrayList of ArrayList's index.(As big as n, here 4)
So,
num / *(pivot) / ArrayList[p]
1: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
2: [2,5,7,9,1,3,4,6,8] -> ArrayList[1]
*
3: [2,5,7,9,1,3,4,6,8] -> ArrayList[2]
*
4: [2,5,7,9,1,3,4,6,8] -> ArrayList[3]
*
5: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
6: [2,5,7,9,1,3,4,6,8] -> ArrayList[1]
*
7: [2,5,7,9,1,3,4,6,8] -> ArrayList[2]
*
8: [2,5,7,9,1,3,4,6,8] -> ArrayList[3]
*
9: [2,5,7,9,1,3,4,6,8] -> ArrayList[0]
*
edited Nov 13 '18 at 10:12
answered Nov 13 '18 at 10:00
c-anc-an
567426
567426
add a comment |
add a comment |
Given a number of bins.
List<List<Integer>> result = new ArrayList<>();
while(result.size()<bins)
result.add(new ArrayList<>());
int counter = 0;
for(Integer i: input)
result.get(counter++%bins).add(i);
add a comment |
Given a number of bins.
List<List<Integer>> result = new ArrayList<>();
while(result.size()<bins)
result.add(new ArrayList<>());
int counter = 0;
for(Integer i: input)
result.get(counter++%bins).add(i);
add a comment |
Given a number of bins.
List<List<Integer>> result = new ArrayList<>();
while(result.size()<bins)
result.add(new ArrayList<>());
int counter = 0;
for(Integer i: input)
result.get(counter++%bins).add(i);
Given a number of bins.
List<List<Integer>> result = new ArrayList<>();
while(result.size()<bins)
result.add(new ArrayList<>());
int counter = 0;
for(Integer i: input)
result.get(counter++%bins).add(i);
answered Nov 13 '18 at 9:27
mattmatt
4,33411226
4,33411226
add a comment |
add a comment |
In Guava library there is a List.Partition method.
List<List<Integer>> split(List<Integer> list, int n)
return Lists.partition(list, n);
This is not what OP wants. Have a close look at the expected output. The order is different.
– Socowi
Nov 13 '18 at 9:28
add a comment |
In Guava library there is a List.Partition method.
List<List<Integer>> split(List<Integer> list, int n)
return Lists.partition(list, n);
This is not what OP wants. Have a close look at the expected output. The order is different.
– Socowi
Nov 13 '18 at 9:28
add a comment |
In Guava library there is a List.Partition method.
List<List<Integer>> split(List<Integer> list, int n)
return Lists.partition(list, n);
In Guava library there is a List.Partition method.
List<List<Integer>> split(List<Integer> list, int n)
return Lists.partition(list, n);
answered Nov 13 '18 at 9:27
Khalid ShahKhalid Shah
1,62921021
1,62921021
This is not what OP wants. Have a close look at the expected output. The order is different.
– Socowi
Nov 13 '18 at 9:28
add a comment |
This is not what OP wants. Have a close look at the expected output. The order is different.
– Socowi
Nov 13 '18 at 9:28
This is not what OP wants. Have a close look at the expected output. The order is different.
– Socowi
Nov 13 '18 at 9:28
This is not what OP wants. Have a close look at the expected output. The order is different.
– Socowi
Nov 13 '18 at 9:28
add a comment |
List<List<Integer>> split(List<Integer> list, int n)
List<List<Integer>> result = new ArrayList<>();
//add n ArrayLists to the result list
IntStream.range(0, n).forEach(k->result.add(new ArrayList<>()));
//iterate over the input list and add one element to one of the inner list of the result array
Iterator i = list.iterator();
while(i.hasNext())
int count = 0;
while(i.hasNext() && count < n)
result.get(count).add((Integer) i.next());
count++;
return result;
add a comment |
List<List<Integer>> split(List<Integer> list, int n)
List<List<Integer>> result = new ArrayList<>();
//add n ArrayLists to the result list
IntStream.range(0, n).forEach(k->result.add(new ArrayList<>()));
//iterate over the input list and add one element to one of the inner list of the result array
Iterator i = list.iterator();
while(i.hasNext())
int count = 0;
while(i.hasNext() && count < n)
result.get(count).add((Integer) i.next());
count++;
return result;
add a comment |
List<List<Integer>> split(List<Integer> list, int n)
List<List<Integer>> result = new ArrayList<>();
//add n ArrayLists to the result list
IntStream.range(0, n).forEach(k->result.add(new ArrayList<>()));
//iterate over the input list and add one element to one of the inner list of the result array
Iterator i = list.iterator();
while(i.hasNext())
int count = 0;
while(i.hasNext() && count < n)
result.get(count).add((Integer) i.next());
count++;
return result;
List<List<Integer>> split(List<Integer> list, int n)
List<List<Integer>> result = new ArrayList<>();
//add n ArrayLists to the result list
IntStream.range(0, n).forEach(k->result.add(new ArrayList<>()));
//iterate over the input list and add one element to one of the inner list of the result array
Iterator i = list.iterator();
while(i.hasNext())
int count = 0;
while(i.hasNext() && count < n)
result.get(count).add((Integer) i.next());
count++;
return result;
answered Nov 13 '18 at 11:17
EritreanEritrean
3,80511015
3,80511015
add a comment |
add a comment |
1
Do you want a random separation?
– Alon
Nov 13 '18 at 9:25
1
@Turamarth the order is not same
– Jimmy Guo
Nov 13 '18 at 9:25
1
@Alon sample averagely
– Jimmy Guo
Nov 13 '18 at 9:25
2
What have you tried so far? I don't see any problems here. Implementing
splitis rather straight forward.– Socowi
Nov 13 '18 at 9:27
@Turamarth The input in that case appears to specifying the size of the chunks, where in this case it would be the number of chunks.
– Michael
Nov 13 '18 at 9:56