我在Java中有一个整数数组,我只想使用它的一部分。我知道在Python中,你可以这样做数组[index:],它从索引中返回数组。这在Java中是可能的吗?
当前回答
在Java中数组的长度是不可变的。因此,您需要将所需的部分复制到一个新数组中。 使用java.util.Arrays类中的copyOfRange方法:
int[] newArray = Arrays.copyOfRange(oldArray, startIndex, endIndex);
startIndex是要复制的范围的初始索引,包括。 endIndex是要复制的范围的最终索引,独占。(这个索引可以在数组之外)
例如:
//index 0 1 2 3 4
int[] arr = {10, 20, 30, 40, 50};
Arrays.copyOfRange(arr, 0, 2); // returns {10, 20}
Arrays.copyOfRange(arr, 1, 4); // returns {20, 30, 40}
Arrays.copyOfRange(arr, 2, arr.length); // returns {30, 40, 50} (length = 5)
其他回答
在Java中数组的长度是不可变的。因此,您需要将所需的部分复制到一个新数组中。 使用java.util.Arrays类中的copyOfRange方法:
int[] newArray = Arrays.copyOfRange(oldArray, startIndex, endIndex);
startIndex是要复制的范围的初始索引,包括。 endIndex是要复制的范围的最终索引,独占。(这个索引可以在数组之外)
例如:
//index 0 1 2 3 4
int[] arr = {10, 20, 30, 40, 50};
Arrays.copyOfRange(arr, 0, 2); // returns {10, 20}
Arrays.copyOfRange(arr, 1, 4); // returns {20, 30, 40}
Arrays.copyOfRange(arr, 2, arr.length); // returns {30, 40, 50} (length = 5)
你可以试试:
System.arraycopy(sourceArray, 0, targetArray, 0, targetArray.length);// copies whole array
// copies elements 1 and 2 from sourceArray to targetArray
System.arraycopy(sourceArray, 1, targetArray, 0, 2);
系统请参见javadoc。
您可以将数组包装为列表,并请求它的子列表。
MyClass[] array = ...;
List<MyClass> subArray = Arrays.asList(array).subList(index, array.length);
如果您使用的是Java 1.6或更高版本,则可以使用数组。copyOfRange复制数组的一部分。来自javadoc:
Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case false is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from.
这里有一个简单的例子:
/**
* @Program that Copies the specified range of the specified array into a new
* array.
* CopyofRange8Array.java
* Author:-RoseIndia Team
* Date:-15-May-2008
*/
import java.util.*;
public class CopyofRange8Array {
public static void main(String[] args) {
//creating a short array
Object T[]={"Rose","India","Net","Limited","Rohini"};
// //Copies the specified short array upto specified range,
Object T1[] = Arrays.copyOfRange(T, 1,5);
for (int i = 0; i < T1.length; i++)
//Displaying the Copied short array upto specified range
System.out.println(T1[i]);
}
}
是的,你可以使用Arrays.copyOfRange
它做的是同样的事情(注意有一个副本:你不改变初始数组)。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 使用C返回一个数组
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 向对象数组添加属性
- 在Java流是peek真的只是调试?