This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList and integers

 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer[] array = new Integer[lotnum.size()];
lotnum.toArray(array);
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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")));
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic