Results 1 to 8 of 8
Thread: Adding elements to an ArrayList
- 01-26-2011, 10:17 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
Adding elements to an ArrayList
ProblemJava Code:/** * Calculates the gross waste emission */ public void calcGrossWasteEmission() { // gross waste emission equals CO2 generated by individual in household times number of people household.add(1018 * household.get(0)); // ERROR: Operator * cannot be applied to int, java.lang.Object }SamDesired solution is to be able to add an element to ArrayList household! :)Last edited by ArcherSam; 01-26-2011 at 10:19 AM. Reason: style
- 01-26-2011, 10:32 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-26-2011, 10:48 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
Type for household Object
The type of household is an ArrayList object.
Java Code:private ArrayList household;
Also changing the ArrayList to Integer won't allow me to add bool type to the list. I'm required to use an array with int and bool elements. Is another solution possible?
SamLast edited by ArcherSam; 01-26-2011 at 10:56 AM. Reason: incomplete request
- 01-26-2011, 10:52 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-26-2011, 11:02 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
Thank You!
Sam
- 01-26-2011, 11:23 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-28-2011, 02:19 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
AFAIK you can’t do such a thing!
You can have your ArrayList generic with no specific type but what you insert into the list MUST be something which is valid for the “*” operator.
The Boolean is not!
You could have tried changing the code to:
household.add(1018 * (Integer) household.get(0));
You basically tell the compiler: “I know what I am doing just cast the Object to Integer and you can have my word for it”.
But during runtime if the household.get(0) contains a Boolean the cast will fail and a ClassCastException will be thrown.
Now the question that should be asked is: “What are you trying to do? Adding Booleans to Integers is not something we do every day”.
Would you like Boolean.False to be considered as “0”? What about Boolean.True? “1”? “-1”?
Think of what you want to do and post back.
- 01-28-2011, 03:05 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Of course you can't do that but it's not the fault of the List<?> and that's what the OP wanted: a List that is capable of storing Booleans and Integers. That's the reason I wrote to use the 'instanceof' operator before casting an element from that list and manipulate it.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Problem in adding arrayList elements
By cool in forum New To JavaReplies: 3Last Post: 01-03-2011, 12:27 PM -
Adding the Elements of a 2D Array
By RMcLuckie45 in forum New To JavaReplies: 0Last Post: 11-07-2010, 11:04 PM -
Cannot swap elements in ArrayList
By glchau in forum New To JavaReplies: 4Last Post: 05-30-2010, 08:03 AM -
Accessing elements of an ArrayList which contains 2D arrays
By Willi in forum New To JavaReplies: 5Last Post: 01-18-2010, 07:00 AM -
Adding elements to an Object Array
By aneesahamedaa in forum New To JavaReplies: 4Last Post: 09-07-2008, 03:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks