简体   繁体   English

Java中的索引语法等效于[1:]

[英]Index syntax in Java equivalent to [1:]

In python, you can do arr[1:] which means from index 1 to end. 在python中,您可以执行arr[1:] ,这意味着从索引1到结束。 Is there an equivalent in Java? Java是否有等效功能?

The closest I can think of is 我能想到的最接近的是

for (int i : arr.length) {
   ...
}

But that doesn't iterate through the array within the input of a java array ( arr[] ) 但这并不会遍历java数组( arr[] )输入中的数组。

假设arr[1:]返回数组arr的副本,您将在其中获得从索引1到末尾的所有元素,请使用

Arrays.copyOfRange(arr, 1, arr.length);

You would have to use an index: 您将必须使用索引:

for (int i=1 ; i<arr.length ; i++) {
    ...
}

Using the enhanced for loop ( for (int i : arr) {...} ) goes through the whole array. 使用增强的for循环( for (int i : arr) {...} )遍历整个数组。

Off the top of my head the array must implement a collection for the enhanced loop to work 在我的头上,数组必须实现一个集合,以使增强的循环正常工作

for (int i : arr) {}

// syntax
for (DataType identifier : arrayIdentifier) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM