Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8877015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:21:48+00:00 2026-06-14T19:21:48+00:00

I have the following code: import java.util.*; public class Lab7 { /** * @param

  • 0

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:21:50+00:00Added an answer on June 14, 2026 at 7:21 pm

    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)};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code in Java: import java.util.*; public class longest{ public static void
I have following code: import java.io.*; import java.util.concurrent.* ; public class Example{ public static
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
I have the following code: import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.util.Locale; public final class
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
I have the following code: import java.util.*; public class SellTransaction extends Transaction { private
I have the following code: package application; import java.util.Timer; import java.util.TimerTask; public class Application
I have the following Java code: import java.util.concurrent.*; class Foo{ static Semaphore s =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.