-1

I would like to know if it is possible to get max length of a row in java 2D array

for example:

my Array : {{1,2,3,4},{1,2,3,4,5},{1,2,3}};

So I have array that has 3 columns (I can get it with Array.length) and max lenght of a row is 5. How would I get this number

2
  • Please have a look at the basic java documentation. Commented Feb 20, 2015 at 19:29
  • 2
    Duplicate??? stackoverflow.com/questions/13665461/… Commented Feb 20, 2015 at 19:31

1 Answer 1

1

You need to iterate through all the rows to find the maximum length:

int max = 0; 
for (int i = 0; i < myArray.length; i++) {
    if (myArray[i].length > max) {
        max = myArray[i].length;
    }
}
return max;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.