SławomirLenart:
In [1]: range(-100, 100, 20)
Out[1]: [-100, -80, -60, -40, -20, 0, 20, 40, 60, 80]
作成する最も簡単な方法は何Array
のJavaの標準ライブラリを使用しての代わりに、独自の機能を足す上記のようにしますか?
ありIntStream.range(-100, 100)
ますが、ステップは1にハードコードされています。
このIS NOT A DUPLICATE のJava:Pythonの範囲の等価(int型、int型)?、私は必要なのでstep
(オフセット)の間の数字をし、内蔵の代わりにサードパーティのライブラリのライブラリを使用するJavaにしたいです。私は自分自身を追加する前にその質問と回答をチェックしました。違いは微妙ですが不可欠です。
user11044402:
使用してIntStream::range
(あなたの特別な工程のために働く必要があります20
)。
IntStream.range(-100, 100).filter(i -> i % 20 == 0);
負のステップを許可する一般的な実装は次のようになります。
/**
* Generate a range of {@code Integer}s as a {@code Stream<Integer>} including
* the left border and excluding the right border.
*
* @param fromInclusive left border, included
* @param toExclusive right border, excluded
* @param step the step, can be negative
* @return the range
*/
public static Stream<Integer> rangeStream(int fromInclusive,
int toExclusive, int step) {
// If the step is negative, we generate the stream by reverting all operations.
// For this we use the sign of the step.
int sign = step < 0 ? -1 : 1;
return IntStream.range(sign * fromInclusive, sign * toExclusive)
.filter(i -> (i - sign * fromInclusive) % (sign * step) == 0)
.map(i -> sign * i)
.boxed();
}
参照https://gist.github.com/lutzhorn/9338f3c43b249a618285ccb2028cc4b5の詳細なバージョンのために。