Question

Which of the following can print out all elements in array? int[] [] items = { {0, 1, 3, 4}, {4, 3, 99, 0, 7 }, {3, 2} }; for
0 0
Add a comment Improve this question Transcribed image text
Answer #1

[Ans] 4th option

for(int row = 0; row < items.length; row++)
{
    System.out.println();
    for(int col=0; col < items[row].length; col++)
        System.out.print(items[row][col]+" ");
}

Execution Results:

1 Sjavac Main.java 3 $java-Xmx128M-Xms16M Main { 0134 43 99 07 32 public class Main 2. { public static void main(String []arg

This code is correct, row correctly represent the array index and col correctly represent the index of element in the row index array. And the condition to terminate the inner for loop is correct. Inner loop takes a row th array and prints all of its elements side by side. While the outer loop changes row index from 0 to items.length - 1 and change the reference from one array to the next. Thus all the elements of all arrays are printed as in the above image.

Reasons for why options a, b, c are wrong:

Option a: Inner loop terminating condition is wrong which is set to the items.lenght but has to be items[row].length

Option b: Inner loop terminating condition is wrong which is set to the items[col].lenght but has to be items[row].length

Option c: It is completely wrong in the inner loop iterating variable is declared as int row instead of int col but used properly in the next printing line.

Thank you! Hit like if you like my work.

Add a comment
Know the answer?
Add Answer to:
Which of the following can print out all elements in array? int[] [] items = {...
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
  • Which of the following is False about arrays in Java? A data structure for storing a...

    Which of the following is False about arrays in Java? A data structure for storing a collection of data of the same type. Length of array can be changed after creation of array. A Java array is an object. Array's index cannot be negative or bigger than array's length. Which of the following can print out all elements in array? int[] [] items - { {0, 1, 3, 4), {4, 3, 99, 0, 7), {3, 2} } ; for (int...

  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

  • please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class...

    please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class Chapter9a_FillInTheCode { public static void main(String[] args) { String [][] geo = {{"MD", "NY", "NJ", "MA", "CA", "MI", "OR"}, {"Detroit", "Newark", "Boston", "Seattle"}}; // ------> exercise 9a1 // this code prints the element at row at index 1 and column at index 2 // your code goes here System.out.println("Done exercise 9a1.\n"); // ------> exercise 9a2 // this code prints all the states in the...

  • whats the answers How many total integers elements are in an array with 4 rows and...

    whats the answers How many total integers elements are in an array with 4 rows and 7 columns? 07 28 11 What is the resulting array contents, assuming that itemList is an array of size 4 having contents -55, -1, 0, 9? for (i = 0; i < 4; ++i) { itemsList[i] - i; -54,0, 1, 10 O 0, 1, 2, 3 O 1, 2, 3, 4 O-55,-1, 0,9 Given an integer array myVals of size N_SIZE (i.e. int[] myVals...

  • 1. What is the output of the following code segment? int array[] = { 8, 6,...

    1. What is the output of the following code segment? int array[] = { 8, 6, 9, 7, 6, 4, 4, 5, 8, 10 }; System.out.println( "Index Value" ); for ( int i = 0; i < array.length; i++ ) System.out.printf( "%d %d\n", i, array[ i ] ); 2. What is the output of the following code segment? char sentence[] = {'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u' }; String output = "The sentence...

  • Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right...

    Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right corner to the bottom left corner and i dont know how to do it please help me thank you dd another method to the bottom of the "TwoDimArraysMethods.java" file called "printDiagonalRL()"                                         public static void printDiagonalRL(int[][] matrix) 4. Call this method in the main file ("TwoDimArraysAsParam.java") passing it "board." e.g. TwoDimArraysMethods.printDiagonal(board); 5. Your new method should print any numbers along the diagonal from...

  • The last element in each array in a 2D array is incorrect. It’s your job to...

    The last element in each array in a 2D array is incorrect. It’s your job to fix each array so that the value 0 is changed to include the correct value. In the first array, the final value should be the length of the first array. In the second array, the final value should be the sum of the first value, and the second to last value in the array. In the third array, the final value should be the...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • Write the code that will print out all elements in the int array called numbers, separated...

    Write the code that will print out all elements in the int array called numbers, separated by spaces, and a newline character after all elements are printed.

  • What elements does the array numbers contain after the following code is executec int[] numbers =...

    What elements does the array numbers contain after the following code is executec int[] numbers = new int [8]; numbers [1] = 3; numbers [4] = 99; numbers [7] = 2; int x = numbers[1]; numbers(x) = 44; numbers (numbers [7]] = 11; O 13, 11, 44, 99, 2] O 10, 3, 11, 44, 99, 0, 0, 2] O 13, 0, 11, 44, 99, 0, 21 O 10, 3, 2, 44, 99, 0, 0, 11]

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