你如何将随机值从一种方法拉到另一种方法?

时间:2021-01-31 19:35:00

long story short. im supposed to write a program with a single-dimension array that holds 10 integer numbers and sort the array using a bubble sort.

长话短说。我应该编写一个程序,其中包含一个包含10个整数的单维数组,并使用冒泡排序对数组进行排序。

now so far I have written:

到目前为止我写了:

System.out.print("The unsorted list is: ");
         int[] numbers = new int[10];       
            //Generates 10 Random Numbers in the range 1 -100
            for(int i = 0; i < numbers.length; i++) {
              numbers[i] = (int)(Math.random() * 100 + 1);
              System.out.print(numbers[i] + " " );
           }//end for loop

but i dont clearly understand how to pass random values from one method to another. the proffesor was kind enough to include a bubble sort code, but im not enterely clear on how its supposed to pull random values from the array in the main method.

但我不清楚如何将随机值从一种方法传递到另一种方法。 proffesor非常友好,可以包含一个冒泡排序代码,但我并不清楚它应该如何从main方法中的数组中提取随机值。

the bubblesort code:

bubblesort代码:

 public static void bubbleSort(int[] list) 
      {
        int temp;

          for (int i = list.length - 1; i > 0; i--) 
          {
             for (int j = 0; j < i; j++) 
             {
               if (list[j] > list[j + 1]) 
               {
               temp = list[j];
               list[j] = list[j + 1];
               list[j + 1] = temp;
               }
             }
          }
       }

any tips or help is greatly apreciated.

任何提示或帮助都是非常有用的。

2 个解决方案

#1


0  

The function

功能

public static void bubbleSort(int[] list)

is expecting int[] list an integer array as parameter, So you pass an integer array.

期望int []列出一个整数数组作为参数,所以你传递一个整数数组。

public static void main(String args[]){
   int[] mList = {12,3,54,67,8,90};
   bubbleSort(mList);
   for(int i = 0 ; i < mList.length ; i++)
      System.out.println(mList[i] + ", ");
}

Just so you know, notice that void main(String[] args) also expects an array (String array) as arguments.

您也知道,请注意void main(String [] args)也需要一个数组(String数组)作为参数。

Also since the argument int[] list is a non primitive (ie. not a plain int, float, char, or their wrapper Object), the arguments are received as reference not values. And so whatever modification is done in the array will be reflected in the main function also.

此外,因为参数int [] list是非原始的(即不是普通的int,float,char或它们的包装器Object),所以参数作为引用而不是值接收。因此,在阵列中进行的任何修改都将反映在main函数中。

#2


0  

Use it like this:

像这样用它:

System.out.print("The unsorted list is: ");
         int[] numbers = new int[10];  
               bubbleSort(numbers);
            //Generates 10 Random Numbers in the range 1 -100
            for(int i = 0; i < numbers.length; i++) {
              numbers[i] = (int)(Math.random() * 100 + 1);
              System.out.print(numbers[i] + " " );
           }//end for loop

 public static void bubbleSort(int[] list) 
      {
        int temp;

          for (int i = list.length - 1; i > 0; i--) 
          {
             for (int j = 0; j < i; j++) 
             {
               if (list[j] > list[j + 1]) 
               {
               temp = list[j];
               list[j] = list[j + 1];
               list[j + 1] = temp;
               }
             }
          }
       }

#1


0  

The function

功能

public static void bubbleSort(int[] list)

is expecting int[] list an integer array as parameter, So you pass an integer array.

期望int []列出一个整数数组作为参数,所以你传递一个整数数组。

public static void main(String args[]){
   int[] mList = {12,3,54,67,8,90};
   bubbleSort(mList);
   for(int i = 0 ; i < mList.length ; i++)
      System.out.println(mList[i] + ", ");
}

Just so you know, notice that void main(String[] args) also expects an array (String array) as arguments.

您也知道,请注意void main(String [] args)也需要一个数组(String数组)作为参数。

Also since the argument int[] list is a non primitive (ie. not a plain int, float, char, or their wrapper Object), the arguments are received as reference not values. And so whatever modification is done in the array will be reflected in the main function also.

此外,因为参数int [] list是非原始的(即不是普通的int,float,char或它们的包装器Object),所以参数作为引用而不是值接收。因此,在阵列中进行的任何修改都将反映在main函数中。

#2


0  

Use it like this:

像这样用它:

System.out.print("The unsorted list is: ");
         int[] numbers = new int[10];  
               bubbleSort(numbers);
            //Generates 10 Random Numbers in the range 1 -100
            for(int i = 0; i < numbers.length; i++) {
              numbers[i] = (int)(Math.random() * 100 + 1);
              System.out.print(numbers[i] + " " );
           }//end for loop

 public static void bubbleSort(int[] list) 
      {
        int temp;

          for (int i = list.length - 1; i > 0; i--) 
          {
             for (int j = 0; j < i; j++) 
             {
               if (list[j] > list[j + 1]) 
               {
               temp = list[j];
               list[j] = list[j + 1];
               list[j + 1] = temp;
               }
             }
          }
       }