116k views
0 votes
printArray is a method that accepts one argument, an array of ints. The method prints the contents of the array; it does not return a value. inventory is an array of ints that has been already declared and filled with values. Write a statement that prints the contents of the array inventory by calling the method printArray.

User Hpityu
by
5.1k points

1 Answer

7 votes

Answer:

// code in java.

// package

import java.util.*;

// class definition

class Main

{

// method to print array elements

public static void PrintArray(int []inventory)

{

// find size of array

int len=inventory.length;

System.out.println("Elements of the array are:");

// print all the elements

for(int x=0;x<len;x++)

{

System.out.print(inventory[x]+" ");

}

}

// mai method

public static void main (String[] args) throws java.lang.Exception

{

try{

// scanner object to read input

Scanner scr=new Scanner(System.in);

System.out.print("Enter the number of elements:");

// read the size of array

int n=scr.nextInt();

// create an array of size n

int inventory[]=new int[n];

System.out.print("Enter the elements of array:");

// read the elements of array

for(int a=0;a<n;a++)

{

inventory[a]=scr.nextInt();

}

// call the method

PrintArray(inventory);

}catch(Exception ex){

return;}

}

}

Step-by-step explanation:

Read the number of elements and assign to "n".Then read the "n" elements and store them into array inventory.Call the method PrintArray()with argument inventory.This method will print all the elements of the array inventory.

Output:

Enter the number of elements:5

Enter the elements of array:3 2 9 6 5

Elements of the array are:

3 2 9 6 5

User EBDOKUM
by
5.7k points