1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Java Collection Help!

Discussion in 'HTML, Graphics & Programming' started by neil3k, 25 Jan 2006.

  1. neil3k


    Joined: 13 Aug 2004

    Posts: 6,762

    Location: Bedford

    Am adding Strings into a Arraylist collection by doing the following

    //add them to the verb array list.
    //System.out.println("added to verbs");
    theVerbs.add(new English(Eng));
    theVerbsfr.add(new French(ActualFrench));

    When i go to print the collection out i get the following however,

    [English@82ba41, English@923e30, English@130c19b, English@1f6a7b9, English@7d772e, English@11b86e7, English@35ce36, English@757aef, English@d9f9c3, English@9cab16, English@1a46e30]

    ideas ?

    I'ts filling the collection but with rubbish ?!
  2. Lagz

    Wise Guy

    Joined: 25 Jul 2003

    Posts: 1,980

    Well your trying to print out the English objects, rather than the strings themselves? Presumable you need to get the string out of the English object and then print it.
  3. xyphic

    Wise Guy

    Joined: 11 Nov 2003

    Posts: 1,694

    Location: South Yorkshire

    It *looks* as though you're simply trying to print out the ArrayList. Because an ArrayList contains a list of Object classes (and *not* English or French classes), then it'll print out like you showed - which is basically an array of Object classes.

    I'm guessing then you're doing something like:

    What you actually want to do is iterate around your ArrayList printing the elements one at a time. Remember that because when you add something to an ArrayList it gets cast down to Object, you have to cast back to the type you expect it to be. Unless you're using Java 5, but we'll not go there just now... Something like this might be what you're after.

    Iterator it;
    it = theVerbs.iterator();
    while (it.hasNext())
    This is completely untested, so no guarantees it'll work! If you still get the funny English@xxxxxx being printed, it indicates that your English class doesn't override the toString() method and you'll either need to implement it, or find some other way of retrieving your string from it.
  4. Dfhaii


    Joined: 19 Oct 2002

    Posts: 550

    Location: Penryn

    Casting (English) is ok if it's an english word coming out, but what about the french ones. It may be easier to have a word class which has a field/enum for english/french and then another variable for the actual word.
    You could also have this as a base class and extend it for english and french but have a common print method.
  5. Hades


    Joined: 19 Oct 2002

    Posts: 24,607

    Location: Surrey

    The collection isn't filled with rubbish. What's happening is that when you print out the colection it's using the ArrayList.toString() method. This is printing out the hashcode of each object. As mentioned above you'll either need to iterate around each of the objects or override the toString() method to do the iteration.