0

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;
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
ClearMist
  • 39
  • 10
  • A void method is simply for executing some commands. A method that returns something is for when you need a value that will be computed over and over. – Isaiah Taylor Nov 09 '13 at 04:13

3 Answers3

1

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.

Steve P.
  • 14,489
  • 8
  • 42
  • 72
1

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
VoidStar
  • 936
  • 1
  • 7
  • 17
  • Oh ok so how would you use withoutResult? since it does not return anything. you pass 5678 to the method, and it executes some code , but how do you use this function non returning function? – ClearMist Nov 09 '13 at 04:39
  • Yes it simply executes the code without giving you a result back ( why depends on the purpose of the function. Maybe the code simply does not generate a result or it is not of interest) – VoidStar Nov 09 '13 at 04:42
  • yes, but the void function i have up there does something important, the insertion sort, if you pass an array to the void inserstion sort it excutes but how do i use the executed function if doesn't give me back anything lol – ClearMist Nov 09 '13 at 04:45
  • You pass an array to the function. The array however is not copied, only its reference is passed. So the function is operating at the original array. This means you do not need to return any value. The result of the code is directly stored in the passed array – VoidStar Nov 09 '13 at 04:50
0

The first method, returning void (i.e., 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
arcy
  • 12,845
  • 12
  • 58
  • 103
  • Nothing in Java passes by reference. – Steve P. Nov 09 '13 at 04:19
  • @Steve P. Sorry -- arrays and objects passed by reference, it was a misleading thing to put there. I've corrected it, and thanks. – arcy Nov 09 '13 at 04:21
  • You still have "What is passed is a reference to an array," but **nothing** in java is [passed by reference](http://stackoverflow.com/questions/40480/is-java-pass-by-reference). – Steve P. Nov 09 '13 at 04:23
  • ok you can pass a reference of an array to the void method, cool. However what is the next step? how to use the result? is more my question – ClearMist Nov 09 '13 at 04:25
  • the return method, i know how to use it's giving something back i can use the return value(arrray) in this point – ClearMist Nov 09 '13 at 04:26
  • When you pass an object (an array is an object in Java) you are passing that reference in to the method. The void method is able to change the object's state (its fields, its elements if it's an array, etc) inside the method. It doesn't need to return anything. It makes the changes "in place". When you have an object in Java you never have the object itself like in some other languages. What you have in Java is a reference to the object, basically a pointer. – Radiodef Nov 09 '13 at 04:32
  • If you print out the values from myArray in the example above, they are in order because the insertionSort method sorted them. I think you may be trying to make this harder than it is. – arcy Nov 09 '13 at 13:19