Java与Python的等价物';s切片

Java与Python的等价物';s切片,java,python,java-8,java-stream,Java,Python,Java 8,Java Stream,正如您所知,如果没有,请浏览一下Python的切片:表示法执行以下操作 [1:5] is equivalent to "from 1 to 5" (5 not included) [1:] is equivalent to "1 to end" a[-1] last item in the array a[-2:] last two items in the array a[:-2] everything except the last two items 我想知道它是通过Java strea

正如您所知,如果没有,请浏览一下Python的切片
表示法执行以下操作

[1:5] is equivalent to "from 1 to 5" (5 not included)
[1:] is equivalent to "1 to end"
a[-1] last item in the array
a[-2:] last two items in the array
a[:-2] everything except the last two items
我想知道它是通过Java streams还是标准API中的其他类似功能实现的,因为它有时确实很有用。

您可以按如下方式使用API:

[1:5]相当于“从1到5”(不包括5)

[1:]相当于“1到结束”

数组中的最后一项[-1]

IntStream.range(list.size() - 1, list.size()) // single item
IntStream.range(list.size() - 2, list.size()) // notice two items
a[-2:]数组中的最后两项

IntStream.range(list.size() - 1, list.size()) // single item
IntStream.range(list.size() - 2, list.size()) // notice two items
a[:-2]除最后两项外的所有内容

IntStream.range(0, list.size() - 2)
注意参数在上下文
范围内​(int startInclusive,int endExclusive)

给定一个整数列表

List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
以上两种方法都可以输出

[2, 3, 4, 5]

太宽泛了,有5个不同的问题。不清楚“或其他什么”是什么意思。标准API中没有这样的内容,但我想实现起来并不复杂,但您必须将
:-2
部分作为字符串传递并解析it@Eugene对于所有用例,
IntStream.range
如何?我的意思不是语法,而是功能。@nullpointer是的,但您仍然需要解析该输入,它也可以通过多种其他方式来完成,我猜
-I
作为python中的索引与
len(array)-I
相同,因此,用Java实现是很简单的。这是假设您也不需要解析功能,例如要解析的
a[:-2]
。我认为他还想从数组中获取值,而不仅仅是生成值。@Andrei yes,exactly@snr以您可能要查找的内容为例进行更新。
List<Integer> subList = list.subList(1, 5);
[2, 3, 4, 5]