Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6339973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:45:00+00:00 2026-05-24T19:45:00+00:00

How do I create a method that takes a two-dimensional array as a parameter

  • 0

How do I create a method that takes a two-dimensional array as a parameter and displays the index of the row with the most zeros? The program I have does compile. It just displays an incorrect result. The method countZeros() counts the number of zeros in each row. I need to compare each count with the next, so I created count and count2. The location of the greater count will be stored in rowNum. I’m not sure what I am doing wrong. I think it may be indexing incorrectly.

Here is my code:

public class P118 
{

    public static void main(String[]args)
    {

        int[][]num = {{0,3,6,0,0}, {1,3,8,9,8}, {9,9,9,0,8}, {3,7,9,9,9}}; 

    System.out.print(rowWithMostZeros(num));

    }

    public static int rowWithMostZeros(int[][]arr)
    {
        int count = 0, count2 = 0, rowNum = -1;

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

            count = countZeros(arr[row]);

            if(count>count2)
            {

            rowNum = row;
            }

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

            count2 = countZeros(arr[i]);

        }

        return rowNum;
    }
    public static int countZeros(int[]x)
    {
        int count = 0;

        for(int i = 0; i<x.length;i++)
        {
            if(x[i]==0)
            {
              count++;

            }
        }


        return count;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:45:02+00:00Added an answer on May 24, 2026 at 7:45 pm

    Try this:

    public static int rowWithMostZeros(int[][] arr) {
    
        if (arr == null || arr.length < 1) {
            return -1;
        }
    
        int rowWithMostZeros = 0;
        int count = countZeros(arr[0]);
    
        for (int i = 1; i < arr.length; i++) {
            int count2 = countZeros(arr[i]);
            if (count2 > count) {
                rowWithMostZeros = i;
            }
        }
    
        return rowWithMostZeros;
    }
    
    public static int countZeros(int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 0) {
                count += 1;
            }
        }
        return count;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a junit test method that takes a CommonsMultipartFile object as a parameter.
I would like to create a method that takes an event as an argument
I am trying to create a unit test for a method that takes a
Im trying to create a method that take 2 int array as the input
I'm trying to create a method that will sum two timeO objects and return
I am trying to create a method that pretty much takes anything as a
I want to create a specific generic method in Scala. It takes two parameters.
I have C function that takes string pointer as a parameter. This functions returns
I have a multi-dimensional array that I am trying to feed into difflib.get_close_matches() .
I'm trying to create a method which takes a String parameter and then returns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.