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

  • Thread starter Thread starter OrbitalPower
  • Start date Start date
  • Tags Tags
    Arrays Java
Click For Summary
The discussion revolves around a method for counting the number of rows in a two-dimensional array of doubles. The initial approach involves a loop that iterates through the array, but participants quickly point out that this method is unnecessarily complex. The consensus is that the number of rows can be directly obtained using `arrayOne.length`, eliminating the need for a loop. There is also clarification that the number of rows in a two-dimensional array is fixed once initialized, though the number of populated elements can vary. For counting columns, it is suggested to use `arrayOne[0].length`. Overall, the conversation emphasizes the importance of efficiency and simplicity in coding solutions.
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:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
Replies
8
Views
2K
  • · Replies 0 ·
Replies
0
Views
627
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 28 ·
Replies
28
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K