How to Count Rows and Columns in a Two-Dimensional Array in Java?

  • Context: Java 
  • Thread starter Thread starter OrbitalPower
  • Start date Start date
  • Tags Tags
    Arrays Java
Click For Summary

Discussion Overview

The discussion revolves around methods for counting the number of rows and columns in a two-dimensional array in Java. Participants explore different approaches and question the efficiency and correctness of proposed solutions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a method that uses a loop to count rows, questioning if there is a simpler solution.
  • Another participant expresses confusion about the need to count from 1 instead of 0, suggesting that it may not be necessary.
  • A third participant points out that the method could simply return the length of the array, questioning the use of a loop at all.
  • Some participants discuss the variability of the number of rows in a two-dimensional array, with one asserting that the size cannot change once set, while another suggests that the number of populated elements may vary.
  • A later reply clarifies that to get the number of columns, one would use the length of the first sub-array.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method for counting rows and columns. There are competing views on the necessity of loops and the implications of array size variability.

Contextual Notes

Some participants mention assumptions about array initialization and population, which may affect the interpretation of the problem. There is also a lack of clarity regarding the definition of "rows" in the context of varying array sizes.

OrbitalPower
I just have a question about my "solution" to a problem. The question says to write a value returning method that returns the number of rows in a two-dimensional array of doubles.

It should be simple enough and so I did it like this:

Code:
public int countRows()
{
	int x;
	x = 0;
	for (int i = 1; i < arrayOne.length; i ++)
		{
			if( i > x)
			x=i;
		}
			x = x+1;
}

arrayOne is the encapsulated array in a class. It just loops through the array and assigns the next i value to the greatest value.

I tested that and it seems to work but it's one of those problems where there probably is a better way. The next question I have is to return the number of columns with two elements, which is why I thought it would be a good idea to get ready to have an if statement in there. Does this look OK or is there a far easier solution?
 
Technology news on Phys.org
I don't understand what you wanting with returning the number of rows in 2D array as you know it can varies.

Anyhow.
Code:
int x;
	x = 0;
	for (int i = 1; i < arrayOne.length; i ++)
Why are you counting from 1?
 
KTC said:
I don't understand what you wanting with returning the number of rows in 2D array as you know it can varies.

I was going to point this out. It is kind of a weird problem but it does come from a programming book. This is just the solution I came up with, and I wondered if anybody else had a better one. I should note it returns x, of course.

KTC said:
Anyhow.
Code:
int x;
	x = 0;
	for (int i = 1; i < arrayOne.length; i ++)
Why are you counting from 1?

Isn't it obvious? Program efficiency.
 
Having actually looked at what you're doing inside that loop...

:confused:

If what're you're returning is just
Code:
arrayOne.length
then why not actually just
Code:
return arrayOne.length
? What's with the loop at all??
 
KTC said:
arrayOne.length then why not actually just return arrayOne.length
This.

Code:
public int countRows(){
	return arrayOne.length;
}

Alternatively, if you wanted the number of rows in a two dimensional array, you would use 'return arrayOne[0].length'. The 'sub' arrays length is indexed by a zero address.

What's this talk about that the number of rows in an array can vary? From my understanding, once the the arrays size(s) have been set, it cannot change. If you're talking about the number elements of the array being populated (that have replaced the initialized values), then you can simply set up a counter for that.
 
Last edited:

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
Replies
8
Views
3K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
20
Views
2K