0

如何将 randomNumbers 的值转换为数组集,以便以后使用它们进行排序?

这是我的主要课程

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;
      }

对于我的随机数类,这里是我的代码。我的问题是如何将值传递randomNumbers给我的主类?

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;
    }

  }
4

2 回答 2

2

int[] 数据=rand.randomNum(randomNumbers); //我是在这里做的吗?

randomNumbers是 subclass 的局部变量RandomAlgo.randomNum()Main不知道。

你可以写你RandomAlgo的如下。

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;
    }

  }

你必须相应地改变你的Main

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();
于 2013-10-03T01:43:06.033 回答
1

randomNum()正在返回您最初请求的随机数的数量,而不是数字本身。更改randomNum()toint[]和方法内部的返回类型,将gHashSet 转换为数组,可能使用此处描述的方法之一,然后返回该数组。

于 2013-10-03T01:39:29.923 回答