Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes ArrayList and integers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList and integers" Watch "ArrayList and integers" New topic
Author

ArrayList and integers

Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
i implemented arraylists and they work with rs.getString, like in:

lotnum.add(num, rs.getString("lotnum"));

but if i do this:
lotnum.add(num, rs.getInt("lotnum"));

it outputs an error saying cannot resolve symbol method add(int,int);
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Originally posted by shuini gustav:
i implemented arraylists and they work with rs.getString, like in:

lotnum.add(num, rs.getString("lotnum"));

but if i do this:
lotnum.add(num, rs.getInt("lotnum"));

it outputs an error saying cannot resolve symbol method add(int,int);

I assume rs is Resultset object, lotnum is Arraylist.

You can add only objects to ArrayList. But Resultset.getInt() will return you int primitive data type. So
lotnum.add(num, rs.getInt("lotnum")); do not work.

You can try
lotnum.add(num, new Integer(rs.getInt("lotnum")));
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
ok. but when i tried to convert it into an array with .toArray(), it says cannot resolve symbol method toArray(int[]);
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Originally posted by shuini gustav:
ok. but when i tried to convert it into an array with .toArray(), it says cannot resolve symbol method toArray(int[]);


Can you provide more details what you trying?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Integer[] array = new Integer[lotnum.size()];
lotnum.toArray(array);


There is no emoticon for what I am feeling!
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
Originally posted by Jeff Albertson:
Integer[] array = new Integer[lotnum.size()];
lotnum.toArray(array);


thanks.

but what is the difference of Integer and int? i mean i know the first one is an object and the second one is a primitive data type but why does ArrayList need objects and not primitive data types?

My database fields from where they are retrieved are of type int and not Integer
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I have no idea what's up with the Integer[] and the toArray(), but you can "wrap" an int in an Integer easily:

lotnum.add(num, new Integer(rs.getInt("lotnum")));


[Jess in Action][AskingGoodQuestions]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by shuini gustav:

but what is the difference of Integer and int? i mean i know the first one is an object and the second one is a primitive data type but why does ArrayList need objects and not primitive data types?

My database fields from where they are retrieved are of type int and not Integer


1. You got it. The difference between java.lang.Integer and int is that the first is a class and the second a primitive type.

2. ArrayList is a list of *objects*. For example the add method is

boolean add(Object object)

because of "upcasting" you can add references to any object to an ArrayList, but primitive values are not objects.

3. As far as database fields go, I prefer representing a number as an Integer versus an int so that I can use Java's null to represent an unset variable or the database NULL value.

Be careful if you are getting values from a nullable field. Calling rs.getInt(field) will return 0 if the field is NULL. Then you have to call rs.wasNull() to distinguish between these two cases.
 
GeeCON Prague 2014
 
subject: ArrayList and integers