Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: ArrayList.t...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 11 Topic 15990 of 16192
Post > Topic >>

Re: ArrayList.toString versus iterating through the ArrayList

by Lew <lew@[EMAIL PROTECTED] > Apr 19, 2008 at 10:52 PM

thufir wrote:
> Hmm, I was taking the example from a book, so that's kinda frustrating. 

> Maybe I misinterpreted it.  I haven't had a chance to google it, but 
> you'd just not create the Guests class which extends ArrayList?

The principle, elucidated in pretty much those words in /Effective Java/
by 
Joshua Bloch, is a guideline rather than a rule.  "Prefer composition"
means 
that in most designs it works better to include an element of type List
than 
to extend a List type.  Sometimes the "is-a" relation****p is so strong and

evident that you absolutely must inherit, but most of the time it makes at

least as much sense to use "has-a", and that makes maintenance easier.

> public class Guests extends ArrayList <Guest>
> {
> 
> }

Since Guests does nothing to add to ArrayList, it probably doesn't need a 
whole new type.

  public class Lab2
  {
   private final List <Guest> guests = new ArrayList <Guest> ();

   public void print()
   {
    for ( Guest g : guests )
    {
      System.out.println( g );
    }
   }
   public boolean add( Guest g )
   {
    if ( g == null )
    { throw new NullPointerException( "null Guest" ); }
    return guests.add( g );
   }
   public static void main( String args [] )
   {
     Lab2 lab = new Lab2();
     for ( int ix = 0; ix < 9; ++ix )
     {
       lab.add( new Guest() );
     }
     lab.print();
   }
  }

-- 
Lew
 




 11 Posts in Topic:
ArrayList.toString versus iterating through the ArrayList
thufir <hawat.thufir@[  2008-04-19 09:41:29 
Re: ArrayList.toString versus iterating through the ArrayList
Hendrik Maryns <gtw37b  2008-04-19 12:18:54 
Re: ArrayList.toString versus iterating through the ArrayList
Lew <lew@[EMAIL PROTEC  2008-04-19 09:18:21 
Re: ArrayList.toString versus iterating through the ArrayList
thufir <hawat.thufir@[  2008-04-19 22:44:33 
Re: ArrayList.toString versus iterating through the ArrayList
Lew <lew@[EMAIL PROTEC  2008-04-19 22:52:07 
Re: ArrayList.toString versus iterating through the ArrayList
Roedy Green <see_websi  2008-04-20 10:37:38 
Re: ArrayList.toString versus iterating through the ArrayList
Roedy Green <see_websi  2008-04-20 10:39:47 
Re: ArrayList.toString versus iterating through the ArrayList
xinxin <jnzhouxinxin@[  2008-04-20 04:41:09 
Re: ArrayList.toString versus iterating through the ArrayList
Lew <lew@[EMAIL PROTEC  2008-04-20 09:36:58 
Re: ArrayList.toString versus iterating through the ArrayList
thufir <hawat.thufir@[  2008-04-20 20:36:09 
Re: ArrayList.toString versus iterating through the ArrayList
thufir <hawat.thufir@[  2008-04-20 20:38:16 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 5 15:02:41 CDT 2008.