简体   繁体   English

Java中二维数组转一维数组

[英]Two dimensional array to one dimensional array in Java

I have a two dimensional array and I fill it with scanner.我有一个二维数组,我用扫描仪填充它。 I want to copy the elements that start with letter 'a' to a new one dimensional array without using ArrayList.我想将以字母 'a' 开头的元素复制到一个新的一维数组中,而不使用 ArrayList。 Please advise on what I can do to get this code functioning properly.请告知我该怎么做才能使此代码正常运行。 the question is how can I know the new array size while I don't know how many words start with letter a问题是我怎么知道新数组的大小,而我不知道有多少单词以字母 a 开头

Here is what I have so far:这是我到目前为止所拥有的:

import java.util.Scanner;

class Untitled {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String[][] name = new String[2][2];

        for (int i = 0; i < name.length; i++) {
            for (int j = 0; j < name[i].length; j++) {

                name[i][j] = input.next();

            }

        }
        student(name);

    }

    public static void student(String[][] arr) {
        int count = 0;
        int c2 = -1;
        String[] name2 = new String[count];
        String temp = "";

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {

                if (arr[i][j].charAt(0) == 'a') {
                    c2++;
                    temp = arr[i][j];
                    name2[c2] = temp;
                    count++;
                    temp = "";
                }

            }//inner

        }//outer

        for (int i = 0; i < name2.length; i++) {
            System.out.println(name2[i]);
        }

    }

}

A two dimensional arrray of size [n][n] is equal to one dimensional array of size n .大小为[n][n] 的二维数组等于大小为n 的一维数组。 If you want to copy them on proper place then you can use this formula, it is useful if you later want to copy these elements back to twodimensional array at proper places:如果您想将它们复制到适当的位置,则可以使用此公式,如果您以后想将这些元素复制回适当位置的二维数组,这将非常有用:

int v = i * n + j; // i and j your loops and n is length of rows or colums.


array[v] = array[i][j];

for in your codes it's like:因为在你的代码中它就像:

int v = 0;

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr[i].length; j++) {

        if (arr[i][j].charAt(0) == 'a') {
             v = i * arra.length +j;
             name2[v] = arr[i][j]; 
                count++;

Ok here is a working code:好的,这是一个工作代码:

public static void main(String [] args) {
     Scanner input = new Scanner(System.in);
        String[][] name = new String[2][2];
        System.out.println("Enter the name: ");
        for (int i = 0; i < name.length; i++) {
            for (int j = 0; j < name[i].length; j++) {

                name[i][j] = input.next();

            }

        }
        student(name);
    }



public static void student(String[][] arr) {
        int count = 0;
        int v = 0;  
        String[] name2 = new String[arr.length*arr[0].length];
        String temp = "";

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {

                if (arr[i][j].charAt(0) == 'a') {
                    v = i *+arr[0].length + j;
                    name2[v] = arr[i][j];
                    count++;
                }

            }//inner

        }//outer

        for (int i = 0; i < name2.length; i++) {
            System.out.println(name2[i]);
        }

        System.out.println("printing without nulls");
        //if you don't want null to be printed then do this:
        for (int i = 0; i < name2.length; i++) {
            if(name2[i] != null)
            System.out.println(name2[i]);
        }
    }

I did it with two nested for loop one for indicating the array size and the other for filling the elements into the array, it does the work but is there any way to do this better我用两个嵌套的 for 循环做了它,一个用于指示数组大小,另一个用于将元素填充到数组中,它可以工作,但有什么方法可以做得更好

public static void student(String[][] arr) {
    int size = 0;

    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {


            if (arr[i][j].charAt(0) == 'a') {
                size++;
            }

        }//inner

    }//outer
    String[] name2 = new String[size];


    int count = 0;

    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {


            if (arr[i][j].charAt(0) == 'a') {
                name2[count] = arr[i][j];
                count++;
            }

        }//inner

    }//outer

    for (int i = 0; i < name2.length; i++) {
        System.out.println(name2[i]);
    }

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

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