![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Posts: 6
Rep Power: 0
![]() |
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
}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]; 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. |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2006
Posts: 6
Rep Power: 0
![]() |
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. ![]() |
|
|
|
|
|
#4 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#5 | |
|
Newbie
|
Quote:
|
|
|
|
|
|
|
#6 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3
![]() |
if you're referring to the serialVersionID, you're off the mark
![]() |
|
|
|
|
|
#7 | |
|
Newbie
|
Quote:
![]() 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! |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|