0

How do I convert the value of randomNumbers to set of array so I could use them for later sorting?

Here's my main class

import java.util.Arrays;
import java.util.*;

public class Main {
  public static void main(String[] args){
    int choice;
    int g;
    Scanner input=new Scanner(System.in);
    System.out.println("Enter how many random no:");
    g=input.nextInt();

    RandomAlgo ran=new RandomAlgo();
    g=ran.randomNum(g);

    RandomAlgo rand=new RandomAlgo();
    int[] data=rand.randomNum(randomNumbers); //did i did this right here? 

  System.out.println("Choose Sorting algorithm: \n (1)Selection Sort \n (2)Insertion Sort \n (3)Bubble Sort \n (4)Quick Sort" );
    choice=input.nextInt();

    switch(choice){
      case 1:{
        SortingAlgo algo=new SortingAlgo();
        data=algo.selectionSort(data);
        break;
      }

for my random numbers class here is my code. My problem is how do I pass the values of randomNumbers to my main class?

public class RandomAlgo extends Main{


  public int randomNum(int g){
        Random rand = new Random();
        int e;
        int i;       
        HashSet<Integer> randomNumbers = new HashSet<Integer>();

        for (i = 0; i < g; i++) {
            e = rand.nextInt(1000);
            randomNumbers.add(e);
            if (randomNumbers.size() <= 0) {
                if (randomNumbers.size() == 0) {
                    g = g;
                }
                g++;
                randomNumbers.add(e);
            }
        }
        System.out.println("Random numbers Are  : " +randomNumbers);
        return g;
    }

  }
n00begon
  • 3,503
  • 3
  • 29
  • 42
  • You do not need to randomize. Just fill with consecutive numbers, and shuffle. See http://stackoverflow.com/questions/4228975/how-to-randomize-arraylist – Sualeh Fatehi Oct 03 '13 at 01:36

2 Answers2

2

int[] data=rand.randomNum(randomNumbers); //did i did this right here?

randomNumbers is a local variable of subclass RandomAlgo.randomNum(), Main don't know about it.

You may write your RandomAlgo as following.

public class RandomAlgo { // Why extend Main ???


  public int[] randomNum(int g){ // return an int array with random numbers instead
        Random rand = new Random();

        int[] randomNumbers = new int[g];

        for (int i = 0; i < g; i++) {
            int e = rand.nextInt(1000);
            randomNumbers[i] = e;

           // if (randomNumbers.size() <= 0) {
             //   if (randomNumbers.size() == 0) {
               //     g = g;
               // }
               // g++;
               // randomNumbers.add(e);
           // }
        }
        // System.out.println("Random numbers Are  : " +randomNumbers);
        return randomNumbers;
    }

  }

You'll have to change your Main accordingly,

public class Main {
  public static void main(String[] args){
    int choice;
    int g;

    Scanner input=new Scanner(System.in);
    System.out.println("Enter how many random no:");
    g=input.nextInt();

    // RandomAlgo ran=new RandomAlgo();
    // g=ran.randomNum(g);

    RandomAlgo rand=new RandomAlgo();
    int[] data=rand.randomNum(g); // load return random numbers array to data

  System.out.println("Choose Sorting algorithm: \n (1)Selection Sort \n (2)Insertion Sort \n (3)Bubble Sort \n (4)Quick Sort" );
    choice=input.nextInt();
TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
1

randomNum() is returning the number of random numbers you initially requested, not the numbers themselves. Change the return type of randomNum() to int[] and inside the method, convert your g HashSet to an array, maybe using one of the methods described here, then return that array.

Community
  • 1
  • 1
Josh
  • 1,563
  • 11
  • 16