11 Replies Latest reply: May 2, 2007 8:21 AM by 807606 RSS

    ArrayList question

    807606
      I am practising programming by trying to write a command-line Jukebox program. The Jukebox is an ArrayList which holds Album objects (I have written my own Abum class).

      Each new Album object has been given the name alb1, alb2, alb3...etc according to it's position in the array. But the toSting() method of the Album class returns the album title (eg. Revolver, Rubber Soul etc). So this code:

      System.out.println(juke.get(1)) //juke is the name of the ArrayList

      will return "Rubber Soul" etc.

      My question is: is there a way of returning the original name of the object (eg alb1) when specifying a position in the ArrayList? So rather than returning "Rubber Soul", it returns "alb1"?
        • 1. Re: ArrayList question
          807606
          you want the instance name of created albums

          i don't think it's possible to retrieve it ; why do you want it?
          • 2. Re: ArrayList question
            807606
            no. consider the code below
            Album alb1 = new Album("Suomi Finland Perkele");
            list.add(alb1);
            From what I gather, you think the above code creates an Album object called "alb1", and adds that object called "alb1" to the list. It doesn't. it creates an Album reference variable called alb1, then adds a copy of that reference variable to the list, and that's an entirely different thing. Objects in java do not have names, and the reference variable you use to manipulate an object isn't the object itself. Consider this:
            Album alb1 = new Album("Suomi Finland Perkele");
            Album alb2 = new Album("The Goat of Mendes");
            Album alb3 = alb1;
            There we have 2 Album objects, and three album references. alb1 and alb3 both point to the same object, but have different names. The actual object itself has no knowledge whatsoever of the references that point to it

            Why do you think you need to resolve back to variable names, anyway? if you're defining Albums in your code like that, that's a poor design. You have to recompile every time you add a new Album! What are you actually trying to achieve?
            • 3. Re: ArrayList question
              807606
              Album alb1 = new Album("Suomi Finland Perkele");
              I, like any finn, love that album.
              • 4. Re: ArrayList question
                807606
                Album alb1 = new Album("Suomi Finland Perkele");
                I, like any finn, love that album.
                are they quite well-known in Finland, then? I've met maybe 5 people who've even heard of them. you finns are quite the rockin' bunch, aren't you! I saw some clips from Finnish Idol on youtube a few weeks ago, bit different to ours (UK)
                • 5. Re: ArrayList question
                  807606
                  If I want to see the tracks on a particular album, I use this:

                  System.out.println(alb1.getTracks());

                  (the tracks are kept in an ArrayList in each Album object)

                  What do I do if I want to see the tracks of the album at position 1 in the jukebox ArrayList?
                  • 6. Re: ArrayList question
                    807606
                    If I want to see the tracks on a particular album, I
                    use this:

                    System.out.println(alb1.getTracks());

                    (the tracks are kept in an ArrayList in each Album
                    object)

                    What do I do if I want to see the tracks of the album
                    at position 1 in the jukebox ArrayList?
                    Album album = list.get(1);
                    System.out.println(album.getTracks());
                    note that I've assumed you're using java 1.5 or later, and generics. that is, your list is declared like this
                    new ArrayList<Album>();
                    if not, you'll need to do
                    Album album = (Album) list.get(1);
                    System.out.println(album.getTracks());
                    • 7. Re: ArrayList question
                      807606
                      Cheers!
                      • 8. Re: ArrayList question
                        807606
                        Actually, that hasn't worked - I get an incompatible types error:

                        Jukebox.java:101:incompatible types

                        found: java.lang.Object
                        required: Album

                        album = juke.get(1);
                        • 9. Re: ArrayList question
                          807606
                          Actually, that hasn't worked - I get an incompatible
                          types error:

                          Jukebox.java:101:incompatible types

                          found: java.lang.Object
                          required: Album

                          album = juke.get(1);
                          if you re-read my previous post, you'll see I anticipated that might happen.....
                          • 10. Re: ArrayList question
                            807606
                            You need to put the generics in the type declaration as well as the new operator as in:
                            List<Album> juke = new ArrayList<Album>(10);
                            • 11. Re: ArrayList question
                              807606
                              Sorted it - cheers again!