2015-10-26 98 views
0
package selectionsortintro; 

public class SelectionSortIntro { 
    public static void main(String[] args) { 
     int nums[] = { 22, 30, 15, 1, 7, 87, 65, 24, 22, 0 }; 

     // print out unsorted list 
     for (int count = 0; count < nums.length; count++) { 
      System.out.print(nums[count] + " "); 
     } 
     System.out.println("\n---------------------------------"); 
     selectionSort(nums); 

     // print out sorted list 
     System.out.println("After sorting using the Selection Sort," + " the array is:"); 
     for (int count = 0; count < nums.length; count++) { 
      System.out.print(nums[count] + " "); 
     } 
    } 

    public static void selectionSort(int data[]) { 
     int smallest; 
     for (int i = 0; i < data.length - 1; i++) { 
      smallest = i; 
      // see if there is a smaller number further in the array 
      for (int index = i + 1; index < data.length; index++) { 
       if (data[index] < data[smallest]) { 
        swap(data, smallest, index); 
       } 
      } 
     } 
    } 

    public static void swap(int array2[], int first, int second) { 
     int hold = array2[first]; 
     array2[first] = array2[second]; 
     array2[second] = hold; 
    } 
} 

我想向数组中添加随机数量的随机整数,所以选择排序算法会将它们整理出来。唯一的问题是,我不知道如何用随机数存储数组,而不是一个固定的数量。如果这是令人困惑的,当你制作数组时,它就像:在数组中存储随机整数

int [] randomNumbers = new int [20];

其中20是产生的数量。那么我想让用户成为判断数组中随机生成了多少个数字的人。所以我想也许使用ArrayList?但是,后来我对如何使用它将随机数添加到自身中感到困惑。如果任何人都可以帮助我,那就太棒了

编辑:所以我得到了输入使用扫描仪,但我真的会更喜欢JOptionPane作为输入对话框看起来好多了,但如果扫描仪是唯一的方式就好了。所以现在完成了,我只需要用随机整数实际填充数组,有谁知道该怎么做?

这就是我想到的,如果有人可以帮助我的代码出错,我会得到一个错误。产生的随机整数,从最简单的

Scanner input = new Scanner(System.in); 

    Scanner s = new Scanner(System.in); 

    System.out.println("enter number of elements"); 

    int n = s.nextInt(); 

    int nums[]=new int[n]; 

    Random randomGenerator = new Random(); 



//print out unsorted list 
for (int count = 0; count < nums.length; count++) { 
    System.out.print(nums[count] + " "); 
    nums[n] = randomGenerator.nextInt(1001); 


} 
+0

可能的重复[Java:创建与随机int的数组(int只能使用一次)](http://stackoverflow.com/questions/7940439/java-create-array-with-random-ints-ints-只能使用一次) –

+2

*我想让用户判断有多少数字随机生成到数组中*:然后开始询问用户他想要多少个数字,然后只创建一个数字适当大小的数组:用户输入的大小。 –

+0

@JBNizet这是我的脑力激荡的一部分,我不知道如何写出数组,而不是固定的数量,它需要用户输入。我得到了铸造和输入错误。 – Jcrow

回答

0

一个字符串变量下面是一个使用传统的阵列和JOptionPane的一个例子:

import javax.swing.*; 
import java.util.Random; 

public class Random_int_array { 

    public static void main(String[] args) { 
     JFrame frame = new JFrame("Total number of integers"); 
     int iTotalCount = Integer.parseInt(JOptionPane.showInputDialog(frame, "What is the total number of integers?")); 

     int[] array = new int[iTotalCount]; 

     Random randomGenerator = new Random(); 

     for(int i=0; i < iTotalCount; i++){ 
      array[i] = randomGenerator.nextInt(1001); 
     } 

     // Now you can do whatever processing you would like to do 
     // For the sake of this answer, I will just print the numbers 

     for(int i=0; i < array.length; i++){ 
      System.out.println(array[i]); 
     } 

     // We should explicitly call exit because we used a form/window 
     System.exit(0); 
    } 
} 

而以下是使用JOptionPane的,而不是常规的int[] array;

import javax.swing.*; 
import java.util.Random; 
import java.util.ArrayList; 

public class Random_int_array { 

    public static void main(String[] args) { 
     JFrame frame = new JFrame("Total number of integers"); 
     int iTotalCount = Integer.parseInt(JOptionPane.showInputDialog(frame, "What is the total number of integers?")); 

     // Can also be written as: ArrayList<Integer> array = new ArrayList<>(); 
     // in newer versions of Java. 
     ArrayList<Integer> array = new ArrayList<Integer>(); 

     Random randomGenerator = new Random(); 

     for(int i=0; i < iTotalCount; i++){ 
      array.add(randomGenerator.nextInt(1001)); 
     } 

     // Now you can do whatever processing you would like to do 
     // For the sake of this answer, I will just print the numbers 

     for(int i=0; i < array.size(); i++){ 
      System.out.println(array.get(i)); 
     } 

     // We should explicitly call exit because we used a form/window 
     System.exit(0); 
    } 
} 
01一个ArrayList的例子

注意:ArrayList s不能使用原始数据类型,因此您必须将其指定为使用Integer而不是int

+0

谢谢,这对我帮助很大!它现在起作用了,谢谢大家回答,你们都很棒。此线程现已解决。 – Jcrow

0

三种不同的方法来实现到最难

生成随机INT [0,最大值)

(int)(Math.random() * max) 

,或者使用

Random r = new Random(); 
r.nextInt(max); 

生成更多随机数而不是Java的伪随机生成器的更复杂方法w应该查询random.org的数据。请注意,设置和编码以及依靠第三方服务器可能需要更长的时间(不管它们的可靠性如何)

您可以使用random int来初始化输入数组长度,则在利用随机数的值填充一个for循环

+2

为什么不是Random.nextInt(max)?更简单,更合适。 –

+0

Random是否有静态方法?我已经习惯将它初始化为多线程目的,并使用Math.random()进行单线程 – phflack

+0

而不是downvoting并保持沉默,为什么/如何更好地回答问题会更有建设性 – phflack

0

添加一个选项来设置所述阵列的基于离用户输入的大小是有点更棘手

来编码它是最简单的方法传入命令行参数并在主方法的args变量中读取它们

另一种读取输入是扫描仪类

您选择哪种方式,你可能最终你需要转换为int与

String input = args[0]; //or use Scanner 
int size = Integer.parseInt(input); 
+0

感谢您的建议。我宁愿使用JOptionPane.showInputDialog,因为它看起来好多了,这仍然有可能吗?扫描仪工作正常,但如果您知道我的意思,我希望有一个更好的外观。此外,现在我得到了它的工作,我只需要实际填充我的数组随机数,我试图将数组分配给我的随机数变量,但我得到错误。 – Jcrow

+0

查看我填充数组的其他答案 - 使用'for(int i = 0; i phflack