[英]What is the fastest way to create integer domain?
I have tried to build domain with integer ranges but excluding one point in this range. 我试图建立具有整数范围的域,但不包括该范围内的一个点。 For example, I should build a range until 4, from always zero.
例如,我应该建立一个一直到零的范围,直到4。 However, I must exclude one point in it say 3. Therefore, my domain should look like array [0,1,2,4].
但是,我必须在其中排除3。因此,我的域应类似于数组[0,1,2,4]。 What is the fastest way to create integer domain?
创建整数域的最快方法是什么?
Create an arraylist of integeres in your domain leaving out one specific element. 在您的域中创建一个整数数组列表,其中不包含一个特定元素。 Something like this:
像这样:
ArrayList<Integer> domain = new ArrayList<Integer>();
createDomain(domain, 1, 4, 3);
...
void createDomain(domain, int start, int end, int leaveOutElement) {
int i;
for (i = start; i <= end; i++) {
if (i == leaveOutElement)
continue;
domain.add(i);
}
}
Using Java 8: 使用Java 8:
final int start = ...;
final int endInclusive = ...;
final int exclude = ...;
int[] domain = IntStream.rangeClosed(start, endInclusive).
filter(i -> i != exclude).
toArray();
ie create an IntStream
[start, endInclusive]
then filter out the value you want to exclude and finally collect it to an array. 即创建一个
IntStream
[start, endInclusive]
然后滤除要排除的值,最后将其收集到数组中。
As a method: 作为一种方法:
public int[] createDomain(final int start, final int endInclusive, final int exclude) {
return IntStream.rangeClosed(start, endInclusive).
filter(i -> i != exclude).
toArray();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.