mardi 17 août 2021

How to compare the lengths of the elements in a column in a two-dimensional jagged array

I was working on this part of the code (printing of the number of elements in column i) I was unsure how to check if there is any other column with a same number of elements as in column i since in my jagged array the user can not enter an array in which there are 2 or more columns with the same number of elements.

package com.company;
import java.util.Scanner;
import java.util.Arrays;
public class Main {
    public static void main(String[] args) {
            Scanner input = new Scanner (System.in);
            //define / make array 
            int coloumns;
            int repeat;
            repeat = 0;
            //take ragged array inout
            do
            {System.out.print("How many coloumns : ");
            coloumns = input.nextInt();
            // define array
            int[][] raggedArray = new int[coloumns][];
            // way to enter ragged array
            for (int i = 0; i < coloumns; i++){
                System.out.print("Enter the number of rows for column number " + (i + 1) + " : ");
                int rows = input.nextInt();
                raggedArray[i] = new int[rows];
                for (int j = 0; j < rows; j++) {
                    System.out.print("Enter row value for row number " + (j + 1) + " : ");
                    //adding the input to array
                    raggedArray[i][j] = input.nextInt();
                }
            }
            // find out the number of columns
/* NEED HELP HERE to compare if there any 2 columns with the same number of elements in them / same number of rows */
            int numberOfColumns = raggedArray.length;
            for (int i = 0; i < numberOfColumns; i++) {
                System.out.println("In the coloumn " + i + " there are " + raggedArray[i].length + " rows");
                }
            } while(repeat == 99);
        }
    }

Aucun commentaire:

Enregistrer un commentaire