Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Arrays with Generic Type (http://www.programmingforums.org/showthread.php?t=10348)

azurik Jun 13th, 2006 8:01 PM

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.

Jimbo Jun 13th, 2006 8:39 PM

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

azurik Jun 13th, 2006 9:19 PM

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. :rolleyes:

Jimbo Jun 13th, 2006 9:35 PM

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...

Gumby Jun 14th, 2006 7:55 AM

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. :rolleyes:

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).

Jimbo Jun 14th, 2006 11:27 AM

if you're referring to the serialVersionID, you're off the mark ;)

Gumby Jun 14th, 2006 12:31 PM

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!


All times are GMT -5. The time now is 1:25 AM.

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