如何将二维数组转换成一维? [英] how to convert 2d array into 1d?

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

问题描述

我有一个code,使用用户输入创建二维数组,它做工精细,但现在我有2个问题

i have a code that create 2d array using user input and it work fine but now i have 2 questions

第一个:如何将二维数组转换成一维数组

the first one: how to convert 2d array to 1d array?

第二个问题:如何选择或跟踪右上方对角线元素的二维数组中。

second question: how to choose or trace the elements above the right diagonal in the 2d array?

任何人都可以帮我解决这个问题code?

anyone can help me to fix this code?

package question3;

import java.util.Arrays;
import java.util.Collection;
import java.util.Scanner;

public class Array2d {

    public static void main(String[] args) {
        int[][] matrix = new int[3][3];

        int[] array = new int[matrix.length * matrix.length];

        Scanner sc = new Scanner(System.in);

        System.out.print("Please enter 9 integers separated by spaces:");
        for (int i = 0; i < matrix.length; i++) {

            for (int j = 0; j < matrix.length; j++) {
                matrix[i][j] = sc.nextInt();
            }

        }

        int idx = 0;

        for (int row = 0; row < matrix.length; row++) {
            for (int column = 0; column < matrix.length; column++) {
                System.out.print(matrix[row][column] + " "); // Outputs the // array in a // 5x5 grid.


            }
            System.out.println();
        }

        for (int column = 0; column < matrix.length; column++) {
            for (int row = column + 1; row < matrix.length+column ; row++){
                // populate your array here
                array[idx] = matrix[row][column];
                // increment index
                idx++;

                System.out.println(matrix[row][column]);
            }

        }       

    }
}

输出

请输入用空格隔开的整数9:
1 2 3 4 5 6 7 8 9

output

Please enter 9 integers separated by spaces: 1 2 3 4 5 6 7 8 9

1 2 3

4 5 6

7 8 9

4
7
8

4 7 8

但我希望2,3,6

在这里,我需要做,因为我坚持,我知道这是在第三循环的变化。

where the change that i need to make because i am stuck and i know that is in the third for loop

推荐答案

那么,如果你运行下面的code,

Well if you run the following code,

public class Main {
    public static void main(String[] args) {
        int[][] matrix = new int[5][6];
        System.out.println(matrix.length);
        int[] matrix2 = matrix[4];
        System.out.println(matrix2.length);
    }
}

您将看到它打印出

5
6

所以,最初您具有 5 的长度,而且有包含数组5 INT [] 的有˛长度 6 每个

So initially you have an array that has a length of 5, and there contains 5 int[] that have a length of ˛6 each.

因此​​它被存储在模式

Therefore it is stored in the pattern of

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30

所以,你想要什么,以便把这些放到一个数组做什么?你需要从左上走向右,再往下,每次一行。

So what do you want to do in order to put these into an array? You need to go from topleft to right, and then down a row each time.

    int newArray[] = new int[matrix.length*matrix[0].length];
    for(int i = 0; i < matrix.length; i++) {
        int[] row = matrix[i];
        for(int j = 0; j < row.length; j++) {
            int number = matrix[i][j];
            newArray[i*row.length+j] = number;
        }
    }

这应该工作。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 

完成code看到自己:

Complete code to see for yourself:

public class Main {
    public static void main(String[] args) {
        int[][] matrix = new int[5][6];

        int counter = 1;
        for(int i = 0; i < 5; i++) {
            for(int j = 0; j < 6; j++) {
                matrix[i][j] = counter++;
            }
        }

        int newArray[] = new int[matrix.length*matrix[0].length];
        for(int i = 0; i < matrix.length; i++) {
            int[] row = matrix[i];
            for(int j = 0; j < row.length; j++) {
                int number = matrix[i][j];
                newArray[i*row.length+j] = number;
            }
        }
        for(int i = 0; i < newArray.length; i++) {
            System.out.print(newArray[i] + " ");
        }
    }
}

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

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