Results 1 to 12 of 12
  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default 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.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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)

  3. #3
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    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...

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Lightbulb keyed collection

    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;
        }
    }
    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.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Quote Originally Posted by carderne View Post
    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...
    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)

  6. #6
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    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...

  7. #7
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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.

  8. #8
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    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.

  9. #9
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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.

  10. #10
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    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?

  11. #11
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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. #12
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

Similar Threads

  1. Replies: 0
    Last Post: 08-12-2008, 01:56 PM
  2. Java Web Developers Needed - Permanent Position
    By ncortel in forum Jobs Offered
    Replies: 0
    Last Post: 08-04-2008, 05:36 AM
  3. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  4. Java Web Developer - Permanent Role - Swindon
    By Mike Brown in forum Jobs Offered
    Replies: 0
    Last Post: 02-12-2008, 04:21 PM
  5. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 07:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •