Array of Vectors

Perhaps this sounds a bit strange, but I think I have a pretty good reason for doing this. I intentionally want to make an array of vectors. Quite frankly, I don't like seeing warnings in my compiler output. So, I'm wondering what I can do about this. I also haven't tested this code to see if it actually works, but I don't see an issue with it.

Declaration:

Vector<Integer> uids =null;

Vector<BackupEntry>[] entries =null;

Initialization (just trust me that uids is initialized correctly):

// Okay we have a list of UIDs. Make the array

entries =new Vector[uids.size()];

// Init each one

for (int i = 0; i < entries.length; i++)

entries[i] =new Vector<BackupEntry>();

This code, however, results in the following warning (with Xlint:unchecked)

Compiling 30 source files to C:\wowiupdater\build\classes

C:\wowiupdater\src\wowiupdater\ReversionDialog.java:62: warning: [unchecked] unchecked conversion

found: java.util.Vector[]

required: java.util.Vector<wowiupdater.BackupEntry>[]

entries = new Vector[uids.size()];

1 warning

Now, I can seewhy it doesn't like this -- because the initialization code doesn't specify what it is a vector of. But, as you can see later in the code, we're certain to be making vectors of BackupEntrys. So I tried this:

entries =new Vector<BackupEntry>[uids.size()];

But that just results in the following complaint:

Compiling 30 source files to C:\wowiupdater\build\classes

C:\wowiupdater\src\wowiupdater\ReversionDialog.java:62: generic array creation

entries = new Vector<BackupEntry>[uids.size()];

1 error

BUILD FAILED (total time: 0 seconds)

So do I have any way around this warning, or do I just have to ignore it? (Or is there a better way I should be going about this?)

Regards,

-- Matt

[2244 byte] By [mpdelbuonoa] at [2007-11-27 0:15:01]
# 1
As a little update I have since tested the system, and it seems to be working correctly. Still though, the question remains, is there a way to get rid of the warning?
mpdelbuonoa at 2007-7-11 22:01:19 > top of Java-index,Java Essentials,New To Java...
# 2

The warning is telling you the compiler can't guarantee the contents of the Vector will always be BackupEntry objects. You can ignore the warning, or even suppress it, but you'll be giving up the added type safety that generics offers. The problem is, generics don't play well with arrays. Do you have to use an array? If you use a Collection instead, there's no problem: List<List<BackupEntry>> entries = null;

...

entries = new ArrayList<List<BackupEntry>>(uids.size());

for (int i = 0; i < uids.size(); i++)

{

entries.add(new ArrayList<BackupEntry>());

}

uncle_alicea at 2007-7-11 22:01:19 > top of Java-index,Java Essentials,New To Java...
# 3

I don't "have" to use an array, no, but I figured that since uids.size() will remain constant, using an array would be preferable since it would perform better than a generic class. Note that, however, it does have to be an array of vectors, and not an array of arrays, because while uids.size() will remain constant, the contents of entries[n] will vary.

Is the performance difference negligable? (The code here is run quite frequently so even small performance differences can cause a large time difference. For example, at one point the code took two minutes to execute. I added an optimization and the time dropped to 15 seconds.)

P.S. Also, how would I go about suppressing a warning message, if I wanted to?

Message was edited by:

mpdelbuono (Adding P.S.)

mpdelbuonoa at 2007-7-11 22:01:19 > top of Java-index,Java Essentials,New To Java...
# 4
@suppressWarnings("unchecked")public List getList() {// etc}
georgemca at 2007-7-11 22:01:19 > top of Java-index,Java Essentials,New To Java...
# 5

Perfect, thank you. What about the question regarding performance? Will there be a noticable difference if I were to use the suggested code instead of an array? (Think about looping it about 200 times, and then consider "noticable")

Thanks again for the generous assistance,

-- Matt

mpdelbuonoa at 2007-7-11 22:01:19 > top of Java-index,Java Essentials,New To Java...
# 6
Vector and ArrayList are both backed by an actual array of objects, so random access on these is pretty much as fast as a regular array.
BinaryDigita at 2007-7-11 22:01:19 > top of Java-index,Java Essentials,New To Java...
# 7

No noticeable difference in performance, I assure you. Assuming you believe that collectively the numerous people who have worked on Java (on the compiler and base classes) are smarter than you, don't try to optimize.

"Premature optimization is the root of all evil" -- Donald Knuth

All optimization is premature without thorough profiling. It is *always* far better to write clear, simple code than to optimize early on by trying to outsmart the compiler.

Herko_ter_Horsta at 2007-7-11 22:01:19 > top of Java-index,Java Essentials,New To Java...