Java - 如何将数组拆分为四个单独的数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9671766/
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
Java - How do I split an array into four separate arrays?
提问by R Doolabh
I have an array which contains a string of numbers such as: 1011
我有一个数组,其中包含一串数字,例如:1011
and I wanted to split it up into four separate arrays containing each of those values. How do I do this?
我想把它分成四个单独的数组,包含每个值。我该怎么做呢?
String [] array = {1,0,1,1};
//would I do something like this:
array.substring(0,4)
回答by Bruno
You should be able to use Arrays.copyOfRange(...)to do this.
您应该能够使用Arrays.copyOfRange(...)来做到这一点。
回答by bbedward
There's no splitting afaik, but you can copy it into new arrays using a range
没有拆分 afaik,但您可以使用范围将其复制到新数组中
check out copyOfRange
查看 copyOfRange
static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType)
usage follows:
用法如下:
String [] array = {1,0,1,1};
// Copies from element 0 to 0
String[] array1 = Arrays.copyOfRange(array, 0, 0);
回答by JuanZe
public static void main(String[] args) {
int[] array = {1,0,1,1};
int[] array1 = Arrays.copyOfRange(array, 0, 1);
int[] array2 = Arrays.copyOfRange(array, 1, 2);
int[] array3 = Arrays.copyOfRange(array, 2, 3);
int[] array4 = Arrays.copyOfRange(array, 3, 4);
System.out.println(array1[0]);
System.out.println(array2[0]);
System.out.println(array3[0]);
System.out.println(array4[0]);
}
回答by csturtz
Option 1 would be to simply create a new array for each item in the array you have... could be done generically like this:
选项 1 是简单地为您拥有的数组中的每个项目创建一个新数组......可以像这样一般地完成:
public <T> List<T[]> splitEachItemIntoOwnArray(T[] original) {
List<T[]> result = new ArrayList<T[]>();
if (original == null) return result;
for (T t : original) {
result.add((T[])new Object[] {t});
}
return result;
}
Option 2, you could also achieve this generically with the Arrays.copyOfRange() method:
选项 2,您也可以使用 Arrays.copyOfRange() 方法来实现这一点:
public <T> List<T[]> splitEachItemIntoOwnArray2(T[] original) {
List<T[]> result = new ArrayList<T[]>();
if (original == null) return result;
for (int i = 0; i < original.length; i++) {
result.add(Arrays.copyOfRange(original,i,i+1));
}
return result;
}
Options 1 and 2 work if you always want the each item of the original array split into its own array. If you might want every N items of the original array split into their own, this should work:
如果您始终希望将原始数组的每个项目拆分为自己的数组,则选项 1 和 2 有效。如果您可能希望将原始数组的每 N 个项目拆分成自己的项目,这应该可行:
public <T> List<T[]> splitEachItemIntoOwnArray2(T[] original, int size) {
List<T[]> result = new ArrayList<T[]>();
if (original == null) return result;
int curPos = 0;
while (curPos < original.length) {
int remaining = original.length - curPos+1;
if (remaining <= size) {
result.add(Arrays.copyOfRange(original,curPos,original.length));
} else {
result.add(Arrays.copyOfRange(original,curPos,curPos+size));
}
curPos += size;
}
return result;
}