Results 1 to 12 of 12
- 12-04-2008, 11:09 AM #1
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Permanent connection to an ArrayList
Say I have an ArrayList of Song objects called lib. Imagine this is my song library.
How I currently have my playlists set up, is that I have a separate Playlist object with another ArrayList of Song objects, called listSongs.
When I add songs to the playlist, it accepts an array of ints and uses this to calculate which Song objects should be duplicated in the listSongs ArrayList.
This works fine, but things get complicated when I do somethings like a delete a song form the main library, because then it has to bugger around deleting the songs from each playlist as well.
I played with keeping the playlist as an array of ints, with the ArrayList only calculated when it needs to be displayed, but this created more trouble.
Is there some way that the Playlist object can rather have a list of connections to the elements in the main lib ArrayList - shortcuts to the Song objects or something?Last edited by carderne; 12-04-2008 at 11:11 AM.
- 12-04-2008, 03:57 PM #2
So lib is an ArrayList of every song you have and listSongs is an ArrayList of Song objects for each Playlist object?
You could attach an ID number to to each song in lib and (as long as it's sorted) use a binary search to check if an ID in playlist exists in lib and if not remove it that way. I don't think there's any way to connect to the elements in the sense you want, and if there is, it's really only doing the same checking and deleting your doing(just behind the scenes)
- 12-04-2008, 08:30 PM #3
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Yes, each listSongs is an ArrayList of Song objects for each playlist.
I originally had the main lib as another listSongs ArrayList, not at all differentiated from each playlist, but although this made displaying the songs easier, it made other things more complex.
What I'm currently doing is similar to what you've said, with each song in lib having a unique ID, which is maintained when the Song object is copied into the listSongs ArrayList. Then when deleting, I check which Song objects in each ArrayList are identical to the songs that have been earmarked for deletion from lib. Your method looks more simple and efficient, so I think I'll give that a go.
I just think that it doesn't make sense to have the actual Song objecti stored in each playlist. Perhaps each Playlist could rather have an Array/ArrayList of ID numbers and then it could have a return method that would calculate, when needed, the required Song objects...
- 12-05-2008, 10:14 AM #4
keyed collection
We can store the song data using the name of the song if the song is able to have placeholder sample data of some fashion.Java Code:public class Loader { public static void main(String[] args) { String[] dontArgue = {"doWab","doWib","doDid","daDont","deyDo"}; CarderneSongSampler css = new CarderneSongSampler(dontArgue); } } class CarderneSongSampler { private Song_objecti[] Songs; public CarderneSongSampler(String[] args) { Keys = new Integer[args.length]; int index = 0; for(String nextName : args) { Songs[index++] = new Song_objecti(nextName); } } public void main() { // ...... } } class Song_objecti { private String name; private SongData songData; public Song_objecti(String songName, byte[] songDataStream) { this.name = songName; this.songData = songDataStream; } }Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 12-08-2008, 04:06 PM #5
That's correct, storing just the ID reference would speed things up a bit(search wise) and be less memory heavy, as opposed to storing the entire object.
Works the same way as Nicholas shows but instead of searching by name you search by number.
Either way a getAttribute() method of some sort will be needed because the song info should be private(so people don't change say Rick Astley - Never Gonna Give You Up ---> Francis Scott Key - The Star-Spangled Banner)
- 12-08-2008, 05:03 PM #6
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Thanks chaps. While I'm here, would you mind answering the followin stick question:
Is it possible to have an instance field that 'redirects' to a static field? So they are one and the same. (I will be making lots of objects of this class, but in of the objects, I want the static field and the instance field to always be the same thing...
- 12-08-2008, 05:21 PM #7
What do you mean by static field? Would that be comparable to a global variable?
instanceField = InstanceObject.getField();
say you were working with the name field.
displayName = Song.getTitle();
you can then concat things to displayName
displayName = displayName+".........";
if for some reason you wanted to do that.
Maybe I'm just not grasping what your actually asking.
- 12-08-2008, 06:37 PM #8
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
No, this is for the List class and objects. Sorry, I don't very up to scratch with all of the nomenclature.
Here's an example of an excerpt from my List class:
private static Song[] mainSongs;
private Song[] listSongs;
Now say I make an object of this class called listFav. Then I will access the name by going:
listFav.getListSongs();
But for one object of this class, I want to access mainSongs, not listSongs. I know I can get around this with if statements, and I know what I want is quite unlikely to exist, but can I, when initializing this special object, make listSongs permanently equal to mainSongs... The static field, mainSongs, contains all of the songs in the library, and is needed for each List class to calculate which Songs it contains.
Hehe, I think I'm asking a bit much.
- 12-08-2008, 08:20 PM #9
You could set it to equal the list at the moment of initialization but it wouldn't auto update whenever mainSongs was updated unless you specifically coded it to update the other one as well.
- 12-08-2008, 10:12 PM #10
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Ok thanks. I thought that maybe, just maybe, there might be some sort of thingy to do it, but it doesn't matter, because I'm going to change the playlists to use an array of songIDs, rather than storing the Song objects themselves.
Rather than make another thread, I have another question which I'll ask here:
I found the javax.sound.sampled package, and I've been tying to use it, without much success so far. First off, can it play mp3 files? I don't think it can, but hopefully I'm wrong. Also, I made an AudioInputStream of a .wav file. Then I made a Clip object and used its open(
) method to open my AudioInputStream and then its loop() method to play it, and although this ran without errors, nothing played and the program terminated immediately...
How can I go about playing some music?
- 12-08-2008, 10:32 PM #11
Your probably better off starting a new thread to deal with just. Some people only read threads with headers that point to a problem they think they can help solve.
I have no idea about the sound package capabilities or AudioInputStream.
Good luck with your project
- 12-09-2008, 09:13 AM #12
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Similar Threads
-
Java developers required for high technology company in Stockport (Permanent roles)
By Rob at Computer People in forum Jobs OfferedReplies: 0Last Post: 08-12-2008, 01:56 PM -
Java Web Developers Needed - Permanent Position
By ncortel in forum Jobs OfferedReplies: 0Last Post: 08-04-2008, 05:36 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
Java Web Developer - Permanent Role - Swindon
By Mike Brown in forum Jobs OfferedReplies: 0Last Post: 02-12-2008, 04:21 PM -
ArrayList
By kizilbas1 in forum New To JavaReplies: 11Last Post: 12-05-2007, 07:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks