Results 1 to 3 of 3

Thread: return array

  1. #1
    rfviki is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default return array

    Hi, i need some help working with an array between funtions
    I need to use bubble sort to order an array, I need to send the array v to bubble function then return it
    In main function it displays the original array than the same array ordered with bubble


    My code is:

    Java Code:
    public class bolhex{
    
      void bubble(int v[]) {
    	System.out.println(" entrou");
     
            for (int i = v.length - 1; i >= 1; i--) {
                for (int j = 1; j <= i; j++) {
                    if (v[j - 1] > v[j]) {
                        int aux = v[j];
                        v[j] = v[j - 1];
                        v[j - 1] = aux;
                    }
                }
            }
    		return v[];
        }
    
    
    public static void main(String [] args){
    int j;
    int [] v=new int[] {3, 6, 5, 1, 9, 7};
    
    for( j = 0 ; j< v.length ;  j++)
    		System.out.println(" "+v[j]);
    
    		bubble(v);
    
    for( j = 0 ; j< v.length ;  j++)
    		System.out.println(" "+v[j]);
    		
    }
    }
    Any help is welcome
    Tanks :)

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    here is the code that runs

    Java Code:
    public class bolhex{
    
    // since you call the bubble from a static method also the bubble must
    // be static. in your previous code you declared the return type void so
    // nothing could be returned. so if you want to return your v declare this
    // as the int[]
      static int[] bubble(int v[]) {
    	System.out.println(" entrou");
     
            for (int i = v.length - 1; i >= 1; i--) {
                for (int j = 1; j <= i; j++) {
                    if (v[j - 1] > v[j]) {
                        int aux = v[j];
                        v[j] = v[j - 1];
                        v[j - 1] = aux;
                    }
                }
            }
    		// you don't need to say v[], v is enough
    		return v;
        }
    
    
    public static void main(String [] args){
    int j;
    int [] v=new int[] {3, 6, 5, 1, 9, 7};
    
    for( j = 0 ; j< v.length ;  j++)
    		System.out.println(" "+v[j]);
    
    		bubble(v);
    
    for( j = 0 ; j< v.length ;  j++)
    		System.out.println(" "+v[j]);
    		
    }
    }
    check it out.

  3. #3
    rfviki is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

Similar Threads

  1. return array problem
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-30-2010, 05:08 PM
  2. How do i return a random String from an array?
    By Grendel0 in forum New To Java
    Replies: 9
    Last Post: 03-11-2010, 10:11 AM
  3. Using int/int, 7/5 would return 1
    By zoe in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 11:25 AM
  4. Replies: 2
    Last Post: 04-16-2008, 12:35 PM
  5. if..else..return
    By mqdias in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 04:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •