如何扁平化的二维数组一维数组? [英] How to flatten 2D array to 1D array?

查看:130
本文介绍了如何扁平化的二维数组一维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能在2维数组 INT originalArray [] [] 压平1维数组?

  int类型的[] = {1,2,6,7,2};
    INT B〔] = {} 2,44,55,2;
    INT C [] = {} 2,44,511,33;    INT originalArray [] [] =新的数组[] [] {A,B,C};


解决方案

一个简单的for循环会做,它并不难,但将取决于你笏复制值的顺序。例如(基于这样的事实:在实施例的阵列都具有相同的长度):

  INT [] newArray =新INT [3 *则为a.length];
INT索引= 0;
对于(INT N = 0; N<则为a.length; N ++){
    newArray [指数++] = A [N];
    newArray [指数++] = B [N];
    newArray [指数++] = C [N];
}

或(顺序不同,A,B,C可以是不同的长度)

  INT [] newArray =新INT [则为a.length + b.length个+ c.length];
System.arrayCopy(一,0,newArray,0,则为a.length);
System.arrayCopy(B,0,newArray,则为a.length,b.length个);
System.arrayCopy(C,0,newArray,则为a.length + b.length个,c.length);

How can I flatten the 2 dimensions array int originalArray[][] to 1 dimension array?

    int a [] = {1,2,6,7,2};
    int b [] = {2,44,55,2};
    int c [] = {2,44,511,33};

    int originalArray [][] = new array[][]{a,b,c};

解决方案

A simple for loop will do, it is not difficult, but will depend on the order you wat to copy the values. For instance (based on the fact that in your example the arrays all have the same length):

int[] newArray = new int[3 * a.length];
int index = 0;
for (int n = 0; n < a.length; n++) {
    newArray[index++] = a[n];
    newArray[index++] = b[n];
    newArray[index++] = c[n];
}

or (different order, a, b, c can be of different lengths):

int[] newArray = new int[a.length + b.length + c.length];
System.arrayCopy(a, 0, newArray, 0, a.length);
System.arrayCopy(b, 0, newArray, a.length, b.length);
System.arrayCopy(c, 0, newArray, a.length + b.length, c.length);

这篇关于如何扁平化的二维数组一维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆