Java!- Activity 1. Printing Arrays. In this activity, you will have to implement
ID: 3681304 • Letter: J
Question
Java!- Activity 1. Printing Arrays. In this activity, you will have to implement three printing methods.
Method 1: printArray
Parameter: a 1D array of Strings
Return type: void
This method prints the content of a 1D array of strings, one string per line.
Requirement: You are expected to use a for loop to implement this method.
Method2: printArray
Parameter: a 1D array of integers
Return type: void
This method prints the content of a 1D array of integers, with one space between each number and returning to the next line once done printing all numbers.
Requirement: You are expected to use a for loop to implement this method.
Method3: printArray
Parameter: a 2D array of integers
Return type: void
This method prints the content of a 2D array of integers, one line per row of the 2D array.
Requirement: You are expected to call the method printArray on 1D arrays of integers to implement this method.
Given:
public static void printArray(int[] myArray) {
// your code goes here
}
public static void printArray(int[][] myArray) {
// your code goes here
}
public static void printArray(String[] myArray) {
// your code goes here
}
Method 1: printArray
Parameter: a 1D array of Strings
Return type: void
Explanation / Answer
Program:
=====================================================================
public class PrintArrays {
public static void main(String args[])
{
int oneDArrayInt[] = { 1, 2, 3, 8, 6, 4, 5, 11, 12, 16, 90, 31 };
int[][] twoDArray = {{3,4,5,6,9},{8,5,9,6,3},{1,1,1,1,6}};
String oneDArrayString[] = { "Hello","Good","Morning","Guys"};
System.out.println("Printing 1 D array int.");
System.out.println("=======================");
printArray(oneDArrayInt);
System.out.println("Printing 2 D array using 1 D array.");
System.out.println("===================================");
printArray(twoDArray);
System.out.println("Printing 1 D array String.");
System.out.println("==========================");
printArray(oneDArrayString);
}
public static void printArray(int[] myArray) {
int len= myArray.length;
for(int i=0;i<len;i++)
{
System.out.print(myArray[i]+" ");
}
System.out.println();
}
public static void printArray(int[][] myArray) {
int row= myArray.length;
int col= myArray[0].length;
for(int i=0;i<row;i++)
{
int oneDArrayIntNew[]=new int[col];
for(int j=0;j<col;j++)
{
//You are expected to call the method printArray on 1D arrays of integers to implement this method.
oneDArrayIntNew[j]=myArray[i][j];
}
printArray(oneDArrayIntNew);
}
}
public static void printArray(String[] myArray) {
int len= myArray.length;
for(int i=0;i<len;i++)
{
System.out.print(myArray[i]+" ");
}
System.out.println();
}
}
===================================================================
Output:
--------
Printing 1 D array int.
=======================
1 2 3 8 6 4 5 11 12 16 90 31
Printing 2 D array using 1 D array.
===================================
3 4 5 6 9
8 5 9 6 3
1 1 1 1 6
Printing 1 D array String.
==========================
Hello Good Morning Guys
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.