• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting arrays

 
James Palmer
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
James Palmer
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't think you can do this:

try taking out the first line...
 
James Palmer
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Note that Java 1.5 adds overloaded toString methods to the Arrays class.
-Jeff-
 
James Palmer
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic