Results 1 to 3 of 3
Thread: return array
- 11-03-2010, 12:46 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
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:
Any help is welcomeJava 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]); } }
Tanks :)
- 11-03-2010, 12:53 PM #2
here is the code that runs
check it out.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]); } }
- 11-03-2010, 01:05 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
return array problem
By doha786 in forum New To JavaReplies: 3Last Post: 03-30-2010, 05:08 PM -
How do i return a random String from an array?
By Grendel0 in forum New To JavaReplies: 9Last Post: 03-11-2010, 10:11 AM -
Using int/int, 7/5 would return 1
By zoe in forum New To JavaReplies: 2Last Post: 12-02-2008, 11:25 AM -
return Set .toArray(); method as an array of integers
By maxim in forum New To JavaReplies: 2Last Post: 04-16-2008, 12:35 PM -
if..else..return
By mqdias in forum New To JavaReplies: 1Last Post: 08-10-2007, 04:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks