简体   繁体   中英

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. 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

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 . 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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