Java数组子串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6597557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:05:26  来源:igfitidea点击:

Java array substring

javaarraysselectinstantiation

提问by Will

How can I create/instantiate an array to be equal to the substring of another array, where the size of the substring is unknown:

如何创建/实例化一个数组以等于另一个数组的子字符串,其中子字符串的大小未知:

int n; //some number derived somewhere else

String[] grp = elements[i] to elements[i+n];

采纳答案by NPE

Use Arrays.copyOfRange:

使用Arrays.copyOfRange

public static <T> T[] copyOfRange(T[] original,
                                  int from,
                                  int to)

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.lengthor 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 null 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.

The resulting array is of exactly the same class as the original array.

将指定数组的指定范围复制到新数组中。范围 ( from)的初始索引必须介于零和original.length,之间。值 atoriginal[from]放置在副本的初始元素中(除非from == original.lengthfrom == to)。来自原始数组中后续元素的值被放置到副本中的后续元素中。范围 ( to)的最终索引(必须大于或等于 from)可能大于original.length,在这种情况下,将 null 放置在索引大于或等于 的副本的所有元素中 original.length - from。返回数组的长度将为to - from.

生成的数组与原始数组的类完全相同。

In your case:

在你的情况下:

String[] grp = Arrays.copyOfRange(elements, i, i + n);

回答by jjnguy

You will use Arrays.copyOfRange().

您将使用Arrays.copyOfRange().

Here is an example:

下面是一个例子:

String[] original = some array;
String[] grp = Arrays.copyOfRange(original, i, i + n);

The Javadocsfor the Arraysclass has lots of information about the method:

Javadoc中Arrays类有许多有关方法的信息:

回答by Joachim Sauer

Use Arrays.copyOfRange():

使用Arrays.copyOfRange()

String[] grp = Arrays.copyOfRange(grp, i, i+n);

As the name implies grpwill be a copyof the original array and not a view into it. You can't have views into the array, for that you'd need to use a collection. Generally speaking collections are the more powerful and flexible, high-level alternative to arrays.

顾名思义,grp将是原始数组的副本,而不是其中的视图。您不能在数组中查看视图,为此您需要使用集合。一般来说,集合是数组的更强大、更灵活、更高级的替代品。

回答by JVM

To get the size of the array, you would do

要获得数组的大小,你会做

String [] grp = new String[n + 1];//inclusive

Then, all you have to do is copy the elements over:

然后,您所要做的就是复制元素:

for(int x = 0;x < n + 1;x++)
{
    grp[x] = elements[i + x];//I'm assuming you have "i" defined somewhere
}