CriCri
CriCri

Reputation: 189

How to return an array

I've been having a problem for the last few hours… I cannot find a answer anywhere so was hoping someone could help me here.

I do not completely understand how i should go about creating an array method that picks random numbers from a method and then displays them in the main method.

The main problem i have been having is how to transfer the method with the random 10 variables in order to substitute the array in the main method so i may display them through the main method?

If any further information is needed please let me know… I really appreciate any help.

the following is just something random i put together that i tried, but doesn't work… i cannot seem to get the method array to compile and transfer to the main method.

import java.util.ArrayList;
public class oneDimensionArraysNew {
public static void main (String [] args) {

    //Declare and create array    
    int[] aVariable =  new int[10];

    randomValues (aVariable);

    System.out.print("Your random variables are" + aVariable);
  }

    public static double randomValues (int random1 []) {
   int[] randomSet = new int[10];

    for (int i = 0; i < randomSet.length; i++) {
        randomSet[i] = (int) Math.random() * 100;//teacher did not specify max
        return randomSet;

Upvotes: 0

Views: 106

Answers (4)

ItachiUchiha
ItachiUchiha

Reputation: 36742

You need to send back the array. Follow this snippet

public static void main (String [] args) {

  //Declare and create array    
  int[] aVariable =  new int[10];
  int[] updatedaVariable = new int[10];

  updatedaVariable = randomValues (aVariable);
  System.out.print("Your random variables are ");

  for(int i : updatedaVariable)
  {
      System.out.print(i + " ");
  }
}

public static int[] randomValues (int random1 []) {
  int[] randomSet = new int[10];

  for (int i = 0; i < randomSet.length; i++) {
      randomSet[i] = (int) (Math.random() * 100);//teacher did not specify max
  }
  return randomSet;
}

Upvotes: 0

Ashish Ratan
Ashish Ratan

Reputation: 2870

change line:

public static double randomValues (int random1 [])

with

public static int[] randomValues (int random1 []) 
or
public static double[] randomValues (int random1 []) {

Upvotes: 0

Martin Dinov
Martin Dinov

Reputation: 8825

Just specify the size of the array of random numbers as an argument to the method, instead of passing an array as you are right now. Also, return a double[] instead of a single double if you want to return an array:

    public static double[] randomValues (int length) {
    int[] randomSet = new int[10];

    for (int i = 0; i < length; i++) {
        randomSet[i] = (int) Math.random() * 100;//teacher did not specify max
    }
    return randomSet;
    }

Upvotes: 0

Smutje
Smutje

Reputation: 18153

Either you return a new array by creating one in the method randomValues (in which case you have to adjust the return type), or you fill the array you have passed in via parameter and return nothing.

Upvotes: 2

Related Questions