0

I'm new to Java, coming from a C/C++ background. I'm trying to write a music player app for Android and I am working on a library scanning function. I want to have a hierarchical database of the format Artist -> Album -> Song where Artist has a name and a group of Albums, an Album has a name, year, and group of Songs, and a Song has a title, track number, and file location.

I created three classes to store this information:

public class libraryElementArtist
{
    public String name;
    public ArrayList<libraryElementAlbum> albums;
}

public class libraryElementAlbum
{
    public String name;
    public String year;
    public ArrayList<libraryElementSong> songs;
}

public class libraryElementSong
{
    public String name;
    public int num;
    public String filename;
}

The idea to fill them is simple - scan through each file and add its artist first, then album, then song. Each time it checks to make sure the artist/album does not already exist before creating a new one.

Essentially, I start off creating an ArrayList to store the artist information like this:

 ArrayList<libraryElementArtist> libraryData = new ArrayList<libraryElementArtist>();

Then, to add an artist to the database:

libraryElementArtist newEntry = new libraryElementArtist();
newEntry.name = song_artist;
libraryData.add(newEntry);

And then to add an album:

libraryElementAlbum newEntry = new libraryElementAlbum();
newEntry.name = song_album;
libraryData.get(artistIndex).albums.add(newEntry);

where artistIndex is the index of the album's artist in the top-level artist array.

When I run this on the device and step through it in the debugger, the libraryElementArtist items are inserted into the libraryData array and their names are correctly filled in. However, the albums field is listed as null and trying to add albums does not fill in any data.

Sorry if this is a noob question, like I said I'm new to Java and I've searched and can't find what I'm looking for. Also, this is the way I'd do such a task in C++, not sure if it's the correct Java way.

4
  • Instead, you should create ArrayList of user-defined objects. Something like ArrayList<Songs> Commented Dec 3, 2011 at 6:05
  • I added a line to the adding artist section: newEntry.albums = new ArrayList<libraryElementAlbum>(); This made an array of Object[0] element show up under albums but this element does not populate still. Commented Dec 3, 2011 at 6:34
  • Fixed it, it was a stupid off-by-one error on the artistIndex variable. The artist was being created and then when the album code tried to run it was trying to write to a non-existing Artist and failing. For anyone seeing this thread in the future, the above code works fine when all the indexes are correct. Commented Dec 3, 2011 at 6:59
  • In the future, wherever you are using some integer "magic index", consider using a Map instead, with Artist as the key. Commented Dec 3, 2011 at 7:52

3 Answers 3

2

I think is a Inheritance funda, So make your classes hierarchy as like (use of inheritance), So you have to make only one class which has all the derived propertied of its parent class, and make a ArrayList of that class only, So you don't have to make nested ArrayList. (If I am not wrong or if then please explain). :-)

Or generally make only one class which has all the properties, As you described above and use some getter,setter methods for that and using that class make your ArrayList.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 Agree with this. And yes you know why i have suggested to have ArrayList<Songs> kind of arraylist because you just have to manage get/set objects otherwise in other ArrayList<ArrayList<String>> case, you have to manage separate values.
@Paresh Mayani - That's why I suggested for make only one class.
What about the is-a principle? An artist is not an album, an album is not a song, a song is not an album, and an album is not an article. Right? Then how do we apply inheritance here?
If you only have a single ArrayList, how does this maintain hierarchy? Maybe the class inherits from a main class, but the ability to index artist->album->song is broken. The whole point of having lists inside lists is so that one artist can be selected and show all of that artist's albums without an exhaustive search of the array (or multiple searches thereof, building additional temporary structures to store this information as searched). Ideally the data from this structure could be dumped to an XML or XML-like file with minimal effort (since scanning a library takes a while, cache).
0

create a custom class with getters and setters then create an arraylist as that datatype (your custom class)

List<customClass> foo = ArrayList<customClass>

same as the comment above but more info

1 Comment

How is this hierarchical? This would just be storing all information about each song (track, artist, album, name, file path, year) in a single object and storing a ton of those into the array. This is not as easy to navigate and eats up a lot more RAM (as each song stores a copy of artist name, album name, etc). I want it to use as little resources as possible since it's going on Android and not PC. The alternative would be some sort of tree data structure, which is what I'm trying to create using standard arrays as the tree's structure is fixed.
0

All of the constructors should create (not just declare) the ArrayLists. Else they will be null. You can start them at a small size to save space. E.g

songs = new ArrayList(3);

1 Comment

I added a line in the artist creation that does this, it just initializes an empty ArrayList though (new ArrayList<libraryElementAlbum>() ) The problem was something completely different and I got it working though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.