Results 1 to 5 of 5
- 08-14-2011, 09:12 AM #1
Member
- Join Date
- Jul 2011
- Location
- New Delhi,India
- Posts
- 56
- Rep Power
- 0
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-
can you please explain where am i wrong?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); } }
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]
- 08-14-2011, 10:03 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
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).Java Code:List<Integer> numList = new ArrayList<Integer>();
In your snippet try casting the arr[1] to an Integer
And see how it reacts.Java Code:sum = sum+(Integer)arr[1];
- 08-14-2011, 10:17 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Schildt is wrong and the compiler is right.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
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".
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.sum=sum+(int)arr[i];
it didnt work....it was saying that required:int and found:java.lang.Object...
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];
- 08-14-2011, 02:04 PM #4
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.
- 08-15-2011, 10:50 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
Using java.util.ArrayList in J2ME (jre 1.4)
By anam siddiqui in forum CLDC and MIDPReplies: 1Last Post: 07-06-2011, 07:50 AM -
Problem with user defined class in ArrayList
By anders73 in forum New To JavaReplies: 4Last Post: 04-26-2011, 03:59 PM -
Problem implementing java.util.Iterable
By ozzyman in forum New To JavaReplies: 2Last Post: 04-04-2011, 11:12 PM -
Demonstration of Stack class in java.util package
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:33 PM -
How to use Stack class in java.util package
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks