Java: you can put two one-dimensional arrays in a bidemensional array

0

They ask me for a program, this should read the ages of the students of two classes separately, then I have created two arrays, class A, class B, read one class and then the ages of the other class, but the program asks that when they are introduced the ages are compared and if class B has students of the same age or lower than class A, a message must be shown on the screen.

It occurs to me to put the data of the two arrays class A, class B into a two-dimensional array and use the bubble loop to compare the ages with a counter that helps me see if class B will have students < = A classe A, but I do not know if it is possible to put two unidimensional arrays in a two-dimensional array.

I leave the code as far as I've come.

public class ProgramaDeClasesEac {

int[] classesA = null;
int[] classesB = null;
int[][] classesAiB = null;

Scanner scan = new Scanner(System.in);

public static void main(String[] args) {

    ProgramaDeClasesEac programa = new ProgramaDeClasesEac();
    programa.inici();

}

private void inici() {
    intro();
    entraMida();
    entraPosicions();
    imprimeixPosicions();
}

private void intro() {
    System.out.println("Aquest programa et demana que entris la mida de dos clases A y B.\n"
            + "Despres et demanara que entris les edats dels alumnes. \n"
            + "Una vegada estiguien entrades comparara les dues clases y retornara si la classe"
            + "<B> es mes gran que la classe <A>.");
}

private void entraMida() {
    System.out.println("Digam la mida que tenen les clases A i B:");
    if (scan.hasNextInt()) {
        int mida = scan.nextInt();
        classesA = new int[mida];
        classesB = new int[mida];
        classesAiB = new int[mida][mida];
    } else {
        scan.next();
        System.out.print("Aquest valor no és correcte. ");
    }
}

private void entraPosicions() {

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

        System.out.println("introdueix la posicio: " + (i + 1) + " de la clase A:");
        classesA[i] = scan.nextInt();

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

        System.out.println("introdueix la posicio: " + (i + 1) + " de la clase B:");
        classesB[i] = scan.nextInt();

    }

}

private void imprimeixPosicions() {
    System.out.print("Collectio 1: \n ");
    for (int i = 0; i < classesA.length; i++) {

        System.out.print(classesA[i] + " ");
    }
    System.out.println("  ");
    System.out.print("Collectio 2: \n ");
    for (int i = 0; i < classesB.length; i++) {

        System.out.print(classesB[i] + " ");

    }
    System.out.println("  ");
}

private void Compara() {

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

        for (int j = 0; j < classesAiB.length; j++) {

        }
    }

}

}

    
asked by Carlos 21.02.2017 в 10:08
source

2 answers

1

You have a concept error here. Since if you have 10 students in class A and 9 in class B, when you create the two-dimensional array you will have an array of 10x9 ... that is 90 "spaces" not an array of two rows of 10 and 9 as I think you are trying to do . I think that for the simple fact of comparing the classes you do not need more than one for another and traverse the array claseB for each position of claseA , for example:

private void Compara() {

    for (int i = 0; i < classesA.length; i++) {
        for (int j = 0; j < classesB.length; j++) {
            // TO-DO
        }
    }
}
    
answered by 21.02.2017 / 10:18
source
0

Answering your question, it would be like this

classesAiB = {classesA, classesB};

However, if you have to count the number of younger students, I think you are not on track. You have the option of Josep or you can even order the two arrays with Arrays.sort in ascending order and look for where the condition is broken.

    
answered by 21.02.2017 в 10:45