1

I have the following code:

import java.util.*;

public class Lab7 {

/**
 * @param args
 */

public static void main(String[] args) {
    // TODO Auto-generated method stub

    double[][] g = { { 0, 1, 2 }, { 1, 0, 3 }, { 2, 3, 0 } };
    double mst[][] = MST.PrimsMST(g);
    PrintArray(g);                                                                      
    PrintMST(mst);
}

public static void PrintArray(double[][] g) {

    System.out.println(Arrays.deepToString(g));

}

public static void PrintMST(double[][] mst) {

    System.out.println(Arrays.deepToString(mst));

}

}

I really need help in adding a RandomArray method that will create a random array for me (g) as opposed to me having to enter the values of the multidimensional array myself.

Here's a piece of code I used before to create a random array:

I can't seem to modify it however to use in the above code, can anyone help me please?

public static ArrayList<Integer> RandomArray(int n)   {                 // Method called RandomArray which takes a     parameter as an integer  
    ArrayList<Integer> randomArray = new ArrayList<Integer>(n);         // Creates an ArrayList called randomArray, of size 'n'
    Random randNumGenerator = new Random();                             // Creates a random object
    for (int i = 0; i < n; i++){                                        // Creates a for loop which goes from i=0 to 'n'
        randomArray.add(new Integer(randNumGenerator.nextInt(256)));    // Will add a random int from 0 to 255, in the array 'randomArray'
    }
    return randomArray;                                                 // Returns randomArray
}
4
  • 3
    Actually you can't use it in your original code. Because you generated an ArrayList, whereas you need an array of array. Commented Nov 22, 2012 at 18:40
  • You could do ArrayList<ArrayList<Integer> > which uses the old RandomArray for each row. Commented Nov 22, 2012 at 18:41
  • 1
    This question is probably asked, in one form or another, about twice a week. If the version you're cribbing from doesn't work for you, find another one. Commented Nov 22, 2012 at 18:44
  • 1
    @HotLicks Search this site for "Lab7 random" Commented Nov 22, 2012 at 18:45

1 Answer 1

1

Changed your method to fit your needs:

public static double[] randomArray(int n) {
    double[] randomArray = new double[n];
    Random randNumGenerator = new Random();
    for (int i = 0; i < n; i++) {
        randomArray[i] = randNumGenerator.nextDouble() * 256;
    }
    return randomArray;
}

now you could call it like

double[][] g = {randomArray(3), randomArray(3), randomArray(3)};
Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't have to be double; you could cast them to int as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.