繁体   English   中英

如何将二维数组值保存到一维数组

[英]How to save a 2-D array value to a 1-D array

如何将2维数组索引的值传递给android中的1维数组索引?我想将此代码放在开关盒内。

int r1[]= int M[0][0];

您想展平数组

根据您使用的版本,有两种选择!

使用Java 8流:

int[] r1= Stream.of(M).flatMapToInt(IntStream::of).toArray();

Java的旧版本:

int[] r1 = new int[size];
int index = 0;
for (int[] x: M) {    // loop through the rows
    for (int y: x)    // loop through the values
        array[index++] = y;
}

使用ArrayList动态分配值是一种选择:

ArrayList<int> r1 = new ArrayList<>();
case R.id.b1:
    int val=M[0][0]; 
    r1.add(val);

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM