-
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????
-
Override the toString method in your Computer class and return the data you want to see when you print a Computer instance.
-
You mean this:
for(Computer x:com)
System.out.println(x.toString());
-
This
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
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:
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.
-
Thanx