Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 13th, 2006, 9:01 PM   #1
azurik
Newbie
 
Join Date: May 2006
Posts: 6
Rep Power: 0 azurik is on a distinguished road
Arrays with Generic Type

I'm trying to create a simple database inside Java using seperate chain Hashing. The database stores the many products inside a grocery store. In my createDatabase method, however, it flags (using Eclipse) a warning (not syntax error) when I try to initalize the database, an array of type ArrayList<Product>:
public void createDatabase(int maxStorage, double maxLoadFactor) {
   int databaseSize = getClosestPrime((int)(maxStorage / maxLoadFactor));
   ArrayList<Product>[] chainArray = new ArrayList[newDatabaseSize];
   database = chainArray;
}
public int getClosestPrime(int num) {
   //code for finding the first number above the given integer
}
(database is an instance field of type ArrayList<Product>[])
The warning says: "Type safety: The expression of type ArrayList[] needs unchecked conversion to conform to ArrayList<Product>[]".
When I try to fix the suggested problem, a syntax error is flagged:
ArrayList<Product>[] chainArray = new ArrayList<Product>[databaseSize];
The syntax error says: "Cannot create a generic array of ArrayList<Product>".

Is there a way so that not a single warning or syntax flag is shown? I'm assuming the code would run despite the warning flag, but I'd rather remove it if at all possible.
azurik is offline   Reply With Quote
Old Jun 13th, 2006, 9:39 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
I had this problem once and I think it's just a bug in how generics work. For a nasty hack that'll hopefully get it working:
private class ProductArray extends ArrayList<Product> {}; // somewhere in your class
/*...*/
ProductArray[] chainArray = new ProductArray[databaseSize]; // in your method
I don't remember if this was exactly how I did it, but it's pretty ugly either way... :o
Jimbo is offline   Reply With Quote
Old Jun 13th, 2006, 10:19 PM   #3
azurik
Newbie
 
Join Date: May 2006
Posts: 6
Rep Power: 0 azurik is on a distinguished road
Looks like this works:
public void createDatabase(int maxPlayerNumber, double maxLoadFactor) {
    int databaseSize = getClosestPrime((int)(maxPlayerNumber / maxLoadFactor));
    class ProductArray extends ArrayList<Character> { }
    ArrayList<Character>[] chainArray = new ProductArray[databaseSize];
    database = chainArray;
}

Thx. Though it's quite unrelated to that warning, a new warning pops up that I usually see when extending many classes. I've never understood what it meant and I believe it starting popping up all over my code once 1.5 came out. It says: "The serializable class ProductArray does not declare a static final serialVersionUID field of type long". Normally, I just ignore it (sometimes I set serialVersionUID to 0, just to shut it up), but what exactly is "serialVersionUID" supposed to be representing?

EDIT: And yes, that is a quite different approach than I would have ever thought of.
azurik is offline   Reply With Quote
Old Jun 13th, 2006, 10:35 PM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
Quote:
Originally Posted by Java API
The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long...
I usually just ignore it as well, since I hardly ever use serialization when I'm doing stuff in Java...
Jimbo is offline   Reply With Quote
Old Jun 14th, 2006, 8:55 AM   #5
Gumby
Newbie
 
Join Date: Mar 2006
Location: Andover, MA / Rochester, NY / Nanticoke, PA
Posts: 16
Rep Power: 0 Gumby is on a distinguished road
Send a message via AIM to Gumby
Quote:
Originally Posted by azurik
Looks like this works:
public void createDatabase(int maxPlayerNumber, double maxLoadFactor) {
    int databaseSize = getClosestPrime((int)(maxPlayerNumber / maxLoadFactor));
    class ProductArray extends ArrayList<Character> { }
    ArrayList<Character>[] chainArray = new ProductArray[databaseSize];
    database = chainArray;
}

Thx. Though it's quite unrelated to that warning, a new warning pops up that I usually see when extending many classes. I've never understood what it meant and I believe it starting popping up all over my code once 1.5 came out. It says: "The serializable class ProductArray does not declare a static final serialVersionUID field of type long". Normally, I just ignore it (sometimes I set serialVersionUID to 0, just to shut it up), but what exactly is "serialVersionUID" supposed to be representing?

EDIT: And yes, that is a quite different approach than I would have ever thought of.
From how I understand it (and I may be off the mark here), you only need to worry about it if youre going to have concurrent access (many users running the same program at the same time, possibly changing the same data).
Gumby is offline   Reply With Quote
Old Jun 14th, 2006, 12:27 PM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
if you're referring to the serialVersionID, you're off the mark
Jimbo is offline   Reply With Quote
Old Jun 14th, 2006, 1:31 PM   #7
Gumby
Newbie
 
Join Date: Mar 2006
Location: Andover, MA / Rochester, NY / Nanticoke, PA
Posts: 16
Rep Power: 0 Gumby is on a distinguished road
Send a message via AIM to Gumby
Quote:
Originally Posted by Jimbo
if you're referring to the serialVersionID, you're off the mark
so then, what IS the mark? correct my ignorance, please

edit: ah, nevermind - i didn't notice you had quoted the Java API earlier, I had just glanced at it, assumed it was a quote from a previous post, and ignored it. d'oh!
Gumby is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC