Results 1 to 8 of 8
  1. #1
    ArcherSam is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Unhappy Adding elements to an ArrayList

    Problem
    Java 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
        }
    Desired solution is to be able to add an element to ArrayList household! :)
    Sam
    Last edited by ArcherSam; 01-26-2011 at 10:19 AM. Reason: style

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ArcherSam View Post
    Java 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
        }


    Sam
    What is the type of variable 'household'? Is it a raw list? If so each of its elements is of type Object which explains the error message. You should change it to a type List<Integer> ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ArcherSam is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Red face 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?

    Sam
    Attached Thumbnails Attached Thumbnails Adding elements to an ArrayList-class-diagram_co2fromwaste.jpg  
    Last edited by ArcherSam; 01-26-2011 at 10:56 AM. Reason: incomplete request

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ArcherSam View Post
    The type of household is ArrayList object.

    Java Code:
    private ArrayList household;
    Sam
    Please don't be afraid to correct my text. I'm not sure if i'm using the correct terms.
    You household variable stores elements of type Object (hence the error message); change its type to ArrayList<Integer> and the compiler and autoboxing will do the rest.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ArcherSam is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    Thank You!

    Sam

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ArcherSam View Post
    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?
    (this remark was added to the reply by the OP later)

    You can also create a List<?> and you can store every type of object in it. You do have to check the actual type of the element(s) when you get them from the list (use the 'instanceof' operator).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    golan2 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    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.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by golan2 View Post
    AFAIK you cant 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.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Problem in adding arrayList elements
    By cool in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:27 PM
  2. Adding the Elements of a 2D Array
    By RMcLuckie45 in forum New To Java
    Replies: 0
    Last Post: 11-07-2010, 11:04 PM
  3. Cannot swap elements in ArrayList
    By glchau in forum New To Java
    Replies: 4
    Last Post: 05-30-2010, 08:03 AM
  4. Replies: 5
    Last Post: 01-18-2010, 07:00 AM
  5. Adding elements to an Object Array
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 09-07-2008, 03:55 PM

Posting Permissions

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