Results 1 to 5 of 5
  1. #1
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Problem in ArrayList class(java.util.*)

    i have been reading ArrayList class when i was stuck with some concepts....

    i read in Herbert Schildt that we cant pass primitive data type in the argument list of "add" method of List interface....but when i tried to pass 1 as the argument there was no errors...this is my code-

    Java Code:
    import java.util.*;
    
    public class ArrayListToArray {
        public static void main(String[] args) {
            ArrayList a1=new ArrayList();
            int sum=0;
            a1.add(1);
            a1.add(2);
            a1.add(3);
            System.out.println("Contents of a1:"+a1);
            Object arr[]=a1.toArray();
            for(int i=0;i<arr.length;i++){
               sum=sum+((Integer)arr[i]).intValue();
                      
            }
            System.out.println(sum);
        }
        
    }
    can you please explain where am i wrong?

    one more thing when i tried this-

    sum=sum+(int)arr[i];
    it didnt work....it was saying that required:int and found:java.lang.Object...

    please explain both.....thanks in advance....
    Last edited by sunde887; 08-14-2011 at 09:57 AM. Reason: Code tags added, [code]...[/code]

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    From what I've heard by those much smarter than I, Herbert Schildt books are bad. That being said, the advice is incorrect as given by the author. The list can have ints added to it. Thanks to auto-boxing, it will automatically convert the literal number '1', to an Integer object, which allows 1,2,3,etc to be added to a list. The problem with trying to add them is that your array(arr), contains an array of objects. The '+' operator takes 2 numerals(or strings), and performs the correct operation, but you are attempting to add objects(which causes the error).

    You should use generics when using Lists to specify the type that will be inserted and removed in a much simpler fashion.

    Java Code:
    List<Integer> numList = new ArrayList<Integer>();
    You simply add <...> to each class name, where ... stands for the class. For all the primitives there exists an equivalent class which is the same name, started with a capital(int=Integer, double=Double, float=Float, etc).

    In your snippet try casting the arr[1] to an Integer

    Java Code:
    sum = sum+(Integer)arr[1];
    And see how it reacts.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    i read in Herbert Schildt that we cant pass primitive data type in the argument list of "add" method of List interface....but when i tried to pass 1 as the argument there was no errors
    Schildt is wrong and the compiler is right.

    What happens is this: you use the primitive int value 1 as an argument and it gets converted to an Integer instance with the value 1. It is a reference to this Integer instance that gets added to the list. This is called "boxing".

    sum=sum+(int)arr[i];
    it didnt work....it was saying that required:int and found:java.lang.Object...
    You declared arr as an array of Object instances. arr[i] is therefore an Object and the compiler is quite rightly complaining. I would have expected it to say something about + being undefined for int and Object, but, in any case, you can't cast an Object into an int. What you have to do is what you have done: cast arr[i] to Integer and then get the value of that Integer instance.

    Note that you can also rely on the opposite of the "boxing" process above. That is, the object (Integer)arr[i] will be automatically "unboxed" to provide the int value. Like this

    Java Code:
    sum = sum + (Integer)arr[i];

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    The place where "boxing" fails is if you add an int , you can't remove it the same way:
    anArrayList.add(234); // this will be boxed into an Integer.
    anArrayList.remove(234); // this will not be boxed, it will look for element 234
    So boxing is tricky and requires the programmer to know when it is happening and when not.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,853
    Rep Power
    19

    Default

    How old is the Schildt book?
    If it's pre 1.5 then it's understandable.

Similar Threads

  1. Using java.util.ArrayList in J2ME (jre 1.4)
    By anam siddiqui in forum CLDC and MIDP
    Replies: 1
    Last Post: 07-06-2011, 07:50 AM
  2. Problem with user defined class in ArrayList
    By anders73 in forum New To Java
    Replies: 4
    Last Post: 04-26-2011, 03:59 PM
  3. Problem implementing java.util.Iterable
    By ozzyman in forum New To Java
    Replies: 2
    Last Post: 04-04-2011, 11:12 PM
  4. Demonstration of Stack class in java.util package
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:33 PM
  5. How to use Stack class in java.util package
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:32 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
  •