[英]Adding chunks of array into array list in java
i want to break an array(with random integers values) into smaller chunks of equal size, and assign the elements based on the value of arrays based. 我想将一个数组(具有随机整数值)分成相等大小的较小块,并根据基于数组的值分配元素。 example if i have an array of size 9, and want to divide it into 3 chunks, the numbers between 0 and 3 are assigned into the first chunk and numbers between 4-6 are assigned into the second and the rest into the third chunk.
例如,如果我有一个大小为9的数组,并想将其分为3个块,则将0和3之间的数字分配给第一个块,将4-6之间的数字分配给第二个块,其余分配给第三个块。 I have to iterate over the separate chunks(can stay for now).
我必须遍历单独的块(可以暂时保留)。 finally add all the chunks into array list.
最后将所有块添加到数组列表中。
int[] originalarray = init[9];
int number = original.length/numerToDivede;
Arraylist<int[]> bigarray = new Arraylist<>();
int[] array1= null;
int[] array2;
int[] array3;
for(int i=0;i<originalarrays.length-1;i++){
if(originalArrays[i] < numberTodivide){
// add to the first array1;
}else if (originalArrays[i] < 2*numberTodivide){
// add to the second array
}else
// add to the third array
this is the way i approached, but every thing is hard coded. 这是我所采用的方法,但是每件事都是硬编码的。 con i do it dynamically?
我动态地做到了吗? appreciate for helping!
感谢您的帮助!
Hope this will help you 希望这个能对您有所帮助
List<Integer> bigarray = new ArrayList<>();
bigarray.addAll(Arrays.asList(1,2,3,4,1,2,3,33,3,4,1,2,1));
List<List<Integer>> outputList = new ArrayList<>();
while(!bigarray.isEmpty()){
List<Integer> tmpList = bigarray.stream()
.filter(item -> item == bigarray.get(0))
.collect(Collectors.toList());
//filter all item has been collect this time
List<Integer> filter = bigarray.stream().filter(item -> item != bigarray.get(0)).collect(Collectors.toList());
bigarray.clear();
bigarray.addAll(filter);
// add the result in this time
outputList.add(tmpList);
}
outputList.forEach(list -> list.forEach(item -> System.out.print(item+", ")));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.