- #1
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:
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?
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?