简体   繁体   中英

how to implement a void method as opposed to a method that returns something?

I am just consfused on how to implement these two methods like how call them or use them? Since the first one is void how does it work?

someone please use and an array and implement this for me or help me understand how the first void method works?

public static void insertionsort(int[] numbers) {
    for (int i = 0; i < numbers.length; i++) {
         int copyNumber = numbers[i];
         int j = i;
         while (j > 0 && copyNumber < numbers[j-1]) {
             numbers[j] = numbers[j-1];
             j--;
         }
         numbers[j] = copyNumber;
    }
}

public int[] InsertionSort(int[] data){
    int len = data.length;
    int key = 0;
    int i = 0;
    for(int j = 1;j<len;j++){
        key = data[j];
        i = j-1;
        while(i>=0 && data[i]>key){
            data[i+1] = data[i];
            i = i-1;
            data[i+1]=key;
        }
    }
    return data;
}

In java everything is passed by value, including references. In your void method, the value of a reference to the array is passed. So while you cannot assign a new int [] to numbers , you are able to change the ints in numbers .

A function with return type does something (executes code) and returns some result back to the code that called that function. A function without return type executes some code but does not return a result ( because it is not needed in most cases )

Consider this two functions:

public static int withResult( int someParameter)
{
    //execute some code here

    int someReturnValue = //result of the code above

    return someReturnValue;
}

public static void withoutResult( int someParameter)
{
    //execute some code here which produces no result which could be of interest to the caller (calling code)
} //end the function without returning anything

You would call it like this:

int result;
result = withResult( 1234 );//executes the function and stores its return type in 'result'
withResult( 468 );//executes the function but does not store the return type anywhere ("throws it away")
withoutResult ( 1234 );//simply executes the function
result = withoutResult ( 5678 ); //this makes no sense because the function does not return anything

The first method, returning void (ie, not returning anything) is passed an array as a parameter. What is passed is a reference to an array that is declared and for which memory is allocated outside the method. The method sorts that information in place; when the method returns, the data in that array is then sorted.

int[] myArray = getArrayInfo();       // assume this gets data in an array
WhateverClass.insertionSort(myArray); // this will sort that data

// at this point, myArray will be sorted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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