Question: Return array from a different method as printed output text. Can someone please show me how to return all of the numbers in an array
Return array from a different method as printed output text. Can someone please show me how to return all of the numbers in an array as text when the array is in a different method? I need to be able to return the array regardless of the amount of numbers in it. For example this only returns 4 numbers when it should be 99. I will be using this println to test several different arrays from the previous method. Please do not suggest better ways to do it as its required for my assignment. The output should always return the full list of numbers in myArray from the M method.
import java.util.*; public class Module1Labs {
public int[] M(int n) {
int[] myArray = new int[n];
for (int i = 0; i < myArray.length; i++) {
myArray[i] = i;
}
return myArray;
}
public static void main(String[] args){
Module1Labs obj = new Module1Labs();
int[] myArray2 = obj.M(5);
System.out.println(Arrays.toString(myArray2));
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
