Question

Please help with the explanation of the of the following code's output: public class ArrayTest {...

Please help with the explanation of the of the following code's output:

public class ArrayTest {

public static void main(String[] args) {

int [][] myArray = {{3,4},{}};

System.out.print(myArray.length + " ");

System.out.print(myArray[0].length + " ");

System.out.print(myArray[1].length + " ");

System.out.print(myArray[0][0] + " ");

Output = 2 2 0 3

Please justfiy this code's output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class ArrayTest {

    public static void main(String[] args) {
        int[][] myArray = {{3, 4}, {}};     // matrix with 2 rows, first row contains values 3, 4 and second row is empty
        System.out.print(myArray.length + " ");     // length of the array is number of row in myArray matrix. (2) 
        System.out.print(myArray[0].length + " ");  // number of elements in first row (2)
        System.out.print(myArray[1].length + " ");  // number of elements in second row (0)
        System.out.print(myArray[0][0] + " ");      // first element in first row (which is 3)
        
        // so, it prints 2 2 0 3
    }
}

Refer to the comments for explanation

Add a comment
Know the answer?
Add Answer to:
Please help with the explanation of the of the following code's output: public class ArrayTest {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT