简体   繁体   English

如何将随机值从一种方法提取到另一种方法?

[英]how do you pull random values from one method into another?

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. 处理器很友好,可以包含一个冒泡排序代码,但是我在主方法中如何从数组中提取随机值一无所知。

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. 非常感谢您提供任何提示或帮助。

The function 功能

public static void bubbleSort(int[] list)

is expecting int[] list an integer array as parameter, So you pass an integer array. 期望int[] list一个整数数组作为参数,因此您传递了一个整数数组。

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)也需要一个数组(字符串数组)作为参数。

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. 同样,由于参数int[] list是非基本参数(即,不是普通的int,float,char或它们的包装对象),因此将参数作为参考而非值接收。 And so whatever modification is done in the array will be reflected in the main function also. 因此,对数组所做的任何修改也将反映在main函数中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将int值从一种方法传递给另一种方法 - How do I pass int values from one method to another 如何使用StringTokenizer从.txt中提取一个单词? - How do you pull one word from a .txt using StringTokenizer? 如何将一个数组的所有值输入到另一个数组的随机索引? - How do i input all the values from one array into random indexes of another array? 如何将更新的全局类变量从一种方法传递到另一种方法? - How do you pass an updated global class variable from one method into another? 如何以一种方法获取从Java中的CSV文件读取的值,并以另一种方法使用它们? - How do I take values read from a CSV file in java in one method and use them in another method? 如何从方法返回值并在另一种方法中调用它? - How do you return a value from a method and call it in another method? 如何将Double值从一个ActionListener方法引入另一个方法来执行计算? Java的 - How do I bring in Double values from one ActionListener method into another to perform a calculation? Java 如何将扫描程序变量从main调用为另一种方法? - How do you call a scanner variable from main into another method? 您如何从另一个类调用方法? - How do you call a method from another class? 如何在另一种方法中调用一个方法的整数? - How do i call an integer from one method in another method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM