I expect this has a very simple solution that I am simply overlooking. I'm hoping that you'll spot it.
I'm working on a data visualization of my iTunes library. It reads the library into memory and creates an array of "Track" object (called "tracks") for each item in the library. Each Track object stores information like the track's title, length, genre, and so on.
I'm interested in the genre right now. So I have thousands of Track objects, and I want to figure out how many unique genres there are (e.g. "Rock," "Electronic" and so on). The genres are stored as Strings within each object.
I thought I would try this: Create a String array and fill it with the genres of all the tracks. Then alphabetize the array, loop through it and do a != comparison to skip over the duplicates. Read out the unique genres into a separate array. Then the second array.length would be the number of unique genres, and the array itself would contain the String text of each genre.
Sounds okay, right? Well, the code below doesn't work. It seems to come up with the correct count in the end, so the uniqueGenres array length is 77, for example, but the contents of each element in the array is blank, or " ". I would expect the elements to be things like "Pop" and "Jazz" (although likely not "Gangsta Rap").
If anyone has some ideas, please let me know. Thanks!
Code:
//Create a new string array to hold the names of all genres (including dupes)
String allGenres[] = new String[tracks.length];
for (int i = 0; i < tracks.length; i++) {
allGenres[i] = tracks[i].genre;
}
//Alphabetize the whole list
allGenres = sort(allGenres);
//Establish unique (non-dupe) genre array, and prepopulate it with the first genre
String uniqueGenres[] = new String[1];
uniqueGenres[0] = tracks[0].genre;
for (int i = 1; i < allGenres.length; i++) {
if (allGenres[i] != allGenres[i-1]) {
}
else {
println(allGenres[i]);
String tempNewGenre = allGenres[i];
uniqueGenres = append(uniqueGenres, tempNewGenre);
}
}
for (int i = 0; i < uniqueGenres.length; i++) {
println(uniqueGenres[i]);
}
println("uniqueGenres.length is " + uniqueGenres.length);