Results 1 to 5 of 5
  1. #1
    nura23 is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default Help with ArrayList

    Hi,

    code:

    /////////////////////////////////////////// ArrayList /////////////////////////
    public void ComputerArrayList(){
    Computer desk1 = new Desktop("Wireless Mouse","Wireless Keyboard","Dell","AMD",2.1,512,"GeForce nVidia 1GB",80,15,500.99);
    Computer desk2 = new Desktop("Wireless Mouse","Without wireless Keyboard","HP","Intel",2.4,1024,"GeForce nVidia 3GB",3200,17,990.00);
    Computer desk3 = new Desktop ("Without wireless Mouse","Without wireless Keyboard","Toshiba","Intel",3.2,4096,"ATI Radeon 3GB",500,25,1590.00);
    Laptop lap1 = new Laptop (8,3.2,"Dell","Intel",2.1,2048,"ATI Radeon 1GB",320,17,590.00);
    Laptop lap2 = new Laptop (6,1.8,"Sony Vaio","Intel",2.4,4096,"GeForce nVidia 4GB",500,15,2590.00);
    Laptop lap3 = new Laptop (6,2.2,"Toshiba","AMD",2.4,4096,"ATI Radeon 2GB",160,15,1590.00);
    ArrayList<Computer> com = new ArrayList<Computer>();
    com.add(desk1);
    com.add(desk2);
    com.add(desk3);
    com.add(lap1);
    com.add(lap2);
    com.add(lap3);

    for(Computer x:com)
    System.out.println(x);
    }
    /////////////////////////////////////////// ArrayList -end /////////////////////
    I try to display the conent of the arrayList but instead i get the ref of the object.
    ?
    Why does it not disply the content of the ArrayList????

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Override the toString method in your Computer class and return the data you want to see when you print a Computer instance.

  3. #3
    nura23 is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default

    You mean this:

    for(Computer x:com)
    System.out.println(x.toString());

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    This
    Java Code:
    Computer desk1 = new Desktop("Wireless Mouse","Wireless Keyboard",
                                 "Dell","AMD",2.1,512,
                                 "GeForce nVidia 1GB",80,15,500.99);
    Computer desk2 = new Desktop("Wireless Mouse","Without wirelessKeyboard",
                                 "HP","Intel",2.4,1024,
                                 "GeForce nVidia 3GB",3200,17,990.00);
    Computer desk3 = new Desktop("Without wireless Mouse",
                                 "Without wirelessKeyboard",
                                 "Toshiba","Intel",3.2,4096,
                                 "ATI Radeon 3GB",500,25,1590.00);
    suggests that your Computer class may have fields such as
    Java Code:
    class Computer
        String mouseType;
        String keyboardType;
        String mfgr;
        String chip;
        // etc
    }
    so a toString override might return some of this field information.
    For example:
    Java Code:
    class Computer {
        String mouseType;
        String keyboardType;
        String mfgr;
        String chip;
        // et al
    
        public String toString() {
            return getClass().getName() + "[" +
                       "mouseType:" + mouseType +
                       ", keyboardType:" + keyboardType +
                       ", mfgr:" + mfgr +
                       ", chip:" + chip + "]";
        }
    }
    For some more ideas about this see Writing toString Methods.

  5. #5
    nura23 is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Regarding arrayList
    By kishan in forum Advanced Java
    Replies: 7
    Last Post: 08-07-2009, 12:48 PM
  2. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  3. ArrayList
    By ramitmehra123 in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 12:47 AM
  4. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 07:30 PM
  5. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 06:45 PM

Tags for this Thread

Posting Permissions

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