简体   繁体   English

如何从void方法打印结果

[英]How can I print a result from a void method

I'm doing an assignment for my intro to programming class and it's a bubble sort. 我正在为我的编程课入门做作业,这是一种冒泡的选择。 The code may be flawed, but I'm not looking for someone to solve that problem. 代码可能有缺陷,但是我不是在寻找解决这个问题的人。 What my problem is, is that I'm trying to print it. 我的问题是,我正在尝试打印它。 I've been given the condition that the method had to be defined by a void method by the line "public static void sort(int[] array)". 我得到的条件是该方法必须由void方法在“公共静态void sort(int []数组)”行中定义。 So, if I tried import Arrays, and used System.out.println(Arrays.toString(sort(array))); 因此,如果我尝试导入Arrays,并使用System.out.println(Arrays.toString(sort(array))); in my main method, it wouldn't work because I get a compiler error saying that void does not apply. 在我的主要方法中,它将不起作用,因为我收到一个编译器错误,指出void不适用。 If I tried to put it into a loop at the main method, it tells me that there are incompatible types. 如果我尝试将其放入main方法的循环中,则表明存在不兼容的类型。 What is found is a void, what is required is an int[], but the original condition of the assignment is to use a void method. 找到的是一个void,所需的是一个int [],但是赋值的原始条件是使用void方法。 So, with that being said, should I change the method from void to int[] for testing purposes and submit the assignment as a void, or is there a way to print the output of this code with the void and I'm just missing it? 因此,话虽如此,我应该出于测试目的将方法从void更改为int []并以void的形式提交分配,还是有办法用void打印此代码的输出,而我只是想念它?

public static void sort(int[] array)
      { 
        int[] y = new int[array.length];
        for(int i = 0; i<=array.length-1; i++)
        {
          for(int j = i+1; i<=array.length-1; j++)
          {
            if(array[i] < array[j]){
              y[i] = array[j];
            }
            if(array[i] >= array[j]){
              y[i] = array[i]; 
          }  
        }
          for(int k = 0; k<y.length; k++){
          System.out.println(y[k]);
          }
      } 
    } //end of sort method

The problem is that you create an new local array inside your sort method which you obviously cannot return because of the restriction of you assignment. 问题在于您在sort方法内创建了一个新的本地数组,由于分配的限制,您显然无法返回该数组。

int[] y = new int[array.length];

Your method will not be valid, since the array passed will remain unchanged. 您的方法将无效,因为传递的数组将保持不变。 You need to do the sorting in place, ie without creating a new array. 您需要进行适当的排序,即无需创建新数组。 So that if you pass it from your main method, it gets sorted and you can print it there. 这样,如果您从主方法传递它,它将被排序并可以在此处打印。

Bubble sort should use a swap method, so you just need one temporary variable. 冒泡排序应使用交换方法,因此您只需要一个临时变量。

In order to print the array, in your main method you'll need 为了打印数组,您需要在main方法中

//code that initializes the array or whatever
sort(myArray);
System.out.println(Arrays.toString(myArray)); //print the array modified by the previous method call

Also note what @A4L said about your local array. 还要注意@ A4L关于本地数组的内容。 Work with the array you pass as parameter to your method, don't create a new one. 使用作为参数传递给方法的数组,不要创建新数组。

Arrays.sort()修改您传入的数组。因此,如果您遍历该array对象并在调用Arrays.sort()之后打印元素,它将为您打印。

Since you have a void sort method, you want it to sort the array as a side effect. 由于您有一个无效的排序方法,因此您希望它对数组进行排序是一种副作用。 That is, modify the values in the int[] array reference you passed as a method parameter. 也就是说,修改您作为方法参数传递的int[] array引用中的值。 Then you can print it from main. 然后您可以从main打印它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM