Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions