简体   繁体   English

Java-如何将一个数组拆分为四个单独的数组?

[英]Java - How do I split an array into four separate arrays?

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)

您应该可以使用Arrays.copyOfRange(...)来执行此操作。

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. 如果您始终希望将原始数组的每个项目拆分为自己的数组,则选项1和2起作用。 If you might want every N items of the original array split into their own, this should work: 如果您可能希望将原始数组的每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;
}

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);
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]);
}

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

相关问题 如何在Java中将字符串数组拆分为单独的数组 - How to split a string array into separate arrays in Java 如何在Java中使用split()字符串方法分隔单词 - How do I separate words using split() string method in Java 如何在Java中分配数组数组? - How do I allocate an array of arrays in Java? 如何将一个数组的值(对于整个数组)分成字母和数字字符,然后分成单独的数组? (Java) - How would I separate one value of an array (for the entire array) into alphabetic and numeric characters, and then into separate arrays? (Java) 如何将String数组拆分为单独的int和String数组? - How to split a String array into separate int and String arrays? JAVA 如何为二维数组编写代码,在 2x2 矩阵中存储四个整数? - JAVA How do I write code for a 2D array storing four integers in a 2x2 matrix? Java如何将数组拆分为两个不同的数组,然后在数组中添加一个数字 - Java How to split array into two different arrays, and then add a number to the arrays 拆分一个数组并将其保存在2个单独的数组中 - Split an array and save them in 2 separate arrays 基于Java中的字长将一组常用英语单词拆分为单独的列表/数组 - Split an array of common English words into separate lists/arrays based on word length in Java Java-将数组数组拆分为多个数组 - Java - Split an array of arrays into multiple arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM