我需要根据图片填写迭代列表(任务)。
要执行任务,从hi
到lo
,差异会生成操作阶乘顺序n!
。
现在,我像图片一样划分任务。优化非常重要。
例如:(80-43) = 37 -> 37! set of complex operations with lists of polynomials and mathematical transformations.
int hi = 80;
int lo = 43;
int expanse = 12;
int overlap = 8;
int remmant = expanse - overlap;
int iterations = (int) Math.ceil((hi - lo - expanse) / (double) remmant) + 1;
List<List<Integer>> listFinishedTask = new ArrayList<>();
for (int i = 0; i < iterations; i++) {
List<Integer> listIterations = new ArrayList<>();
for (int j = (hi - i * (remmant)); j > Math.max(hi - i * (remmant) - expanse, lo - 1); j--) {
listIterations.add(new Integer(j));
}
listFinishedTask.add(listIterations);
}
listFinishedTask.forEach(listIterations -> {
System.out.println(Arrays.toString(listIterations.toArray()));
});
输出为:
[80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69]
[76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65]
[72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61]
[68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57]
[64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53]
[60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49]
[56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45]
[52, 51, 50, 49, 48, 47, 46, 45, 44, 43]
现在,还有另一种算法可以生成类似于图片的列表吗?
答案 0 :(得分:0)
只需滑动它!
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class SlidingTest {
/* public static final class Sliding<T> extends AbstractList<List<T>> {
private final List<T> list;
private final int chunkSize;
private final int overlap;
public Sliding(List<T> list, int chunkSize, int overlap) {
this.list = new ArrayList<>(list);
this.chunkSize = chunkSize;
this.overlap = overlap;
}
@Override
public List<T> get(int index) {
int start = index * (chunkSize - overlap);
int end = Math.min(start + chunkSize, list.size());
if (start > end) {
throw new IndexOutOfBoundsException("Index " + index + " is out of the list range <0," + (size() - 1) + ">");
}
return list.subList(start, end);
}
@Override
public int size() {
return (int) Math.ceil((double) (list.size() + chunkSize * (chunkSize - overlap)) / (double) chunkSize);
}
} */
public static void main(String[] args) {
try {
int hi = 80;
int lo = 43;
int expanse = 12;
int overlap = 8;
int remmant = expanse - overlap;
int iterations = (int) Math.ceil((hi - lo - expanse) / (double) remmant) + 1;
/* List<Integer> numbers = IntStream.rangeClosed(lo, hi).boxed().map(j -> lo + (hi - j)).collect(Collectors.toList());
Sliding<Integer> listFinishedTask = new Sliding<Integer>(numbers, expanse, overlap); */
List<List<Integer>> listFinishedTask = IntStream.range(0, iterations).parallel().mapToObj(i -> {
int start = Math.max(hi - i * (remmant) - expanse + 1, lo);
int end = hi - i * (remmant);
return IntStream.rangeClosed(start, end).boxed()
.map(j -> start + (end - j))
.collect(Collectors.toList());
}).collect(Collectors.toList());
listFinishedTask.forEach(System.out::println);
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
答案 1 :(得分:0)
如何使用List.subList
?这样可以避免很多不必要的复制:
// Create an ArrayList [80, 79, 78, ... , 44, 43]
List<Integer> list = new ArrayList<>();
for (int i = hi; i >= lo; i--) {
list.add(i);
}
// Create an ArrayList of sublists
List<List<Integer>> subLists = new ArrayList<>();
for (int i = 0; i < list.size(); i += remmant) {
subLists.add(list.subList(i, Math.min(list.size(), i + expanse)));
}
// Print each sublist
subLists.forEach(System.out::println);
对于expanse = 12
和overlap = 8
,它将打印:
[80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69]
[76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65]
[72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61]
[68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57]
[64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53]
[60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49]
[56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45]
[52, 51, 50, 49, 48, 47, 46, 45, 44, 43]
[48, 47, 46, 45, 44, 43]
[44, 43]