Re: i need some advice ....
Hello and welcome,
How big array are we talking about?
You might want to write your self a comparator and sort the array on whatever it is you want to sort it on :D
// Json
Re: i need some advice ....
i don't know what is a big array but i hove at least 7000 objects on it how do i create comparator and i don't really understand what the term Collections mean what is it ?
did i understand this write?
sort methods put things in order and comparator are the way they put them in order
if you can give me some explanation that well be great
Re: i need some advice ....
Ok, here we go, lets say we have a separate file/class for the mp3 info.
Code :
public class Mp3Info implements Comparable<Mp3Info> {
private String artist;
private String song;
public Mp3Info() {
}
public Mp3Info(final String artist, final String song) {
this.artist = artist;
this.song = song;
}
@Override
public int compareTo(Mp3Info mp3Info) {
return this.artist.compareTo(mp3Info.getArtist());
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
}
As you can see there it implements Comparable so we have to implement the compareTo method. This could be done using a comparator instead and if you wish to know what that will look like, let me know and I will create one.
Here is the code for my main test class.
Code :
public static void main(String... args) {
final List<Mp3Info> mp3s = new ArrayList<Mp3Info>();
mp3s.add(new Mp3Info("La Roux", "Bulletproof"));
mp3s.add(new Mp3Info("Cascada", "Evacuate the Dancefloor"));
mp3s.add(new Mp3Info("Black Eyed Peas", "I got a feeling"));
mp3s.add(new Mp3Info("Lady Gaga", "Just dance"));
Collections.sort(mp3s);
for (final Mp3Info mp3Info : mp3s) {
System.out.println("Artist: " + mp3Info.getArtist());
}
}
And thats it really.
// Json
Re: i need some advice ....
Ok, lets say we wanted to sort by songs instead, lets create a comparator.
Code :
public static class SongComparator implements Comparator<Mp3Info> {
@Override
public int compare(Mp3Info mp3Info1, Mp3Info mp3Info2) {
return mp3Info1.getSong().compareTo(mp3Info2.getSong());
}
}
And the main app.
Code :
public static void main(String... args) {
final List<Mp3Info> mp3s = new ArrayList<Mp3Info>();
mp3s.add(new Mp3Info("La Roux", "Bulletproof"));
mp3s.add(new Mp3Info("Cascada", "Evacuate the Dancefloor"));
mp3s.add(new Mp3Info("Black Eyed Peas", "I got a feeling"));
mp3s.add(new Mp3Info("Lady Gaga", "Just dance"));
Collections.sort(mp3s, new SongComparator());
for (final Mp3Info mp3Info : mp3s) {
System.out.println("Artist: " + mp3Info.getArtist() + " (" + mp3Info.getSong() + ")");
}
}
Note how we pass in a new SongComparator into the Collections.sort method.
// Json
Re: i need some advice ....
thank you very very much
let me see if i understand the sort method forces the arraylist (that is a abstract of collection) to use the comparator or the Comparable interface to organize the objects ....
...?
Re: i need some advice ....
Well, to sort an arraylist you have to pass it to the Collections static method called sort, for this to work the object in the arraylist must implement comparable or you can pass in another comparator you've created which will take that object as you can see by the generics declaration.
However if you would like to have a list of unique items you can make your self a TreeList which will always sort it self when passing in a new object.
// Json
Re: i need some advice ....
both methods have a compareTo method in them that return 0,1,-1 can i build some other comparing system that returns the same ints ???
Re: i need some advice ....
For creating a quick set, i'd recommend using a Binary Search Tree or a Hash Table instead. You can define the compareTo method any way you want, that's the point of having the Comparable interface. It allows you to compare certain things that for humans may be trivial, but are quite strange for the computer to compare, for example which picture has more blue, or which next move in chess is a good/optimal choice.