aspose file tools*
The moose likes Beginning Java and the fly likes Sorting arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting arrays" Watch "Sorting arrays" New topic
Author

Sorting arrays

James Palmer
Ranch Hand

Joined: Mar 15, 2004
Posts: 36
Hello,i have 3 variables(integer numbers) and i want to make an array to sort them in ascending order but i can't find the correct way to write it.
What i write is(p1,p2,p3 are the names of the variables):
int[] pile = new int[3];
pile[0] = p1;
pile[1] = p2;
pile[2] = p3;
Then to sort and print the variables i do this:
System.out.println(array.sort(int[] pile))
Obviously am doing something wrong cause the compiler comes up with quite a few errors.The array.sort i got it from the API list and i cant understad what am writing wrong.Can anyone help?
Chris Harris
Ranch Hand

Joined: Sep 21, 2003
Posts: 231
Hi James,
When you said you got it for the API did you mean you having been trying to use the Arrays class.
The Arrays class has a lot of helpful methods when handeling arrays. All the methods are static. If you want to sort you ints you could try using the sort method in this class. Here is an example

The array will be sorted. Note that this method does not return anything. As it just sorts the arrays in the param directly. To print out the values try:

Hope that helps. Note I have wittern this code directly into this text box, so it may to be perfect but you should be able to get the idea.
Let us know you have any problems.
Chris


SCJP 1.2, SCWCD, SCBCD
James Palmer
Ranch Hand

Joined: Mar 15, 2004
Posts: 36
Hey Chris and thanks for your help.
I ve still got problems,i ll put here exactly what i put down,you might know what to do:
int[] pile = new int[3];
int [] pile = {p1,p2,p3};
Arrays.sort(pile);
for(int i = 0; i < pile.length; i++) {
System.out.println(pile[i]);
}
p1,p2 and p3 are variables which get integer number values.
It gives me 3 errors,
1.indentifier expected at Arrays.sort(pile)
2.illegal start for type for(int i = 0; i < pile.length; i++) {
3.indentifier expected at the last line which has only the }
Hope you can help and sorry if those questions are quite silly,as you can tell am new!!Thanks
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Without seeing your whole java class source, it looks obvious that you didn't put that code inside a method, but rather simply put it in a class definition, where it does not belong. You need:
class whatever {
// optional variables (not code) go here
// methods go here
void someMethod() {
// code goes here
}
}
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11295
    
  16

i don't think you can do this:

try taking out the first line...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
James Palmer
Ranch Hand

Joined: Mar 15, 2004
Posts: 36
I want to put it in a toString in order to bring out the variables sorted.This is my code:
**********************************
public String toString() {
int[] pile = {p1,p2,p3};
Arrays.sort(pile);
return "Equaliser state: { " + for (int i = 0; i < myArray.length; i++)
{
System.out.print(pile[0]);
System.out.print(",");
System.out.print(pile[1]);
System.out.print(",");
System.out.print(pile[2]);
System.out.println("}");
}
}
**********************
I don't know what am doing wrong again.
All those sytem.out.print are in order to make the variables come out like this:{p1,p2,p3}
Hope someone out there could help.Thanks
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762

Note that Java 1.5 adds overloaded toString methods to the Arrays class.
-Jeff-


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
James Palmer
Ranch Hand

Joined: Mar 15, 2004
Posts: 36
Thanks Jeff but that code is quite complicated for me.
Could i just have an array instead of the variables from the beginnings(get rid of p1,p2,p3) and do it some way in the toString method to bring up the three arrays sorted?I apologize about this,i havent put my question correctly and thanks for your answers.Hope you can help me with this now and not want to kill me!!Thanks to all.
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
Originally posted by James Palmer:
Thanks Jeff but that code is quite complicated for me.
Could i just have an array instead of the variables from the beginnings(get rid of p1,p2,p3) and do it some way in the toString method to bring up the three arrays sorted?

Starting out with an array containing the ints is an improvement. Note though that using Arrays.sort rearranges the int[] in place: Any subsequent times you access the int[], it will be sorted. You may not want that. If not, you can make a copy of the array and sort that copy.
The above code shouldn't have appeared that complicated. The part that probably looks complicated is the asString method. You can rewrite this method to use String concatenation (+) if you're more comfortable with that. Something like:
That should be a bit easier to understand. It's less efficient than using StringBuffer and would generally be frowned upon in this circumstance, but doing it this way is OK for now.
One thing that you were doing in your original code is using System.out.println within the toString method. The goal of a toString method is to construct and return a single string--not to display the output on the console with System.out.println. Some other code can then use that string (and print it on the console if desired).
If this still isn't helping, add more specifics on what's confusing you and on what you need to accomplish.
Jeff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting arrays