array of lists

J

Jeremy Watts

i have created a one-dimensional array of type 'List' , ie. each of the
elements of the array is a list (of integers). The lines i used to do this
being :-

List[] termNumbersArray;
List<Object>termNumbers = new ArrayList<Object>();
.. .
..
.. .
..
then i attempt to add an element (numberOfTerms) to the list at position
'index' with the line :-

termNumbersArray[index].add(numberOfTerms);

It compiles correctly but gives a compiler warning "uses unchecked or unsafe
operations. Note:Recompile with -Xlint:unchecked for details."

How do I get around this?

Thanks
 
A

Andrew Thompson

Jeremy Watts wrote:
....
It compiles correctly but gives a compiler warning "uses unchecked or unsafe
operations. Note:Recompile with -Xlint:unchecked for details."

How do I get around this?

I understand there are switches to suppress warnings,
but it might be more profitable to add '-Xlint:unchecked'
to the compile time options for further.. details 1st.

Andrew T.
 
W

Wesley Hall

Jeremy said:
i have created a one-dimensional array of type 'List' , ie. each of the
elements of the array is a list (of integers). The lines i used to do this
being :-

List[] termNumbersArray;
List<Object>termNumbers = new ArrayList<Object>();
. .
.
. .
.
then i attempt to add an element (numberOfTerms) to the list at position
'index' with the line :-

termNumbersArray[index].add(numberOfTerms);

It compiles correctly but gives a compiler warning "uses unchecked or unsafe
operations. Note:Recompile with -Xlint:unchecked for details."

How do I get around this?

Thanks

If your lists are lists of integers then why are you using Object as the
generic type argument?

List<Integer> termNumbers = new ArrayList<Integer>();

I am not 100% certain, but I have a feeling this will rid you of your
compilation warning.
 
J

Jeremy Watts

Wesley Hall said:
Jeremy said:
i have created a one-dimensional array of type 'List' , ie. each of the
elements of the array is a list (of integers). The lines i used to do this
being :-

List[] termNumbersArray;
List<Object>termNumbers = new ArrayList<Object>();
. .
.
. .
.
then i attempt to add an element (numberOfTerms) to the list at position
'index' with the line :-

termNumbersArray[index].add(numberOfTerms);

It compiles correctly but gives a compiler warning "uses unchecked or unsafe
operations. Note:Recompile with -Xlint:unchecked for details."

How do I get around this?

Thanks

If your lists are lists of integers then why are you using Object as the
generic type argument?

List<Integer> termNumbers = new ArrayList<Integer>();

I am not 100% certain, but I have a feeling this will rid you of your
compilation warning.

cah.. of course... yes i'll try that.. i've been using Object because all of
the lists i've used so far have been needed to store lists of different data
types
 
J

Jeremy Watts

Jeremy Watts said:
Wesley Hall said:
Jeremy said:
i have created a one-dimensional array of type 'List' , ie. each of the
elements of the array is a list (of integers). The lines i used to do this
being :-

List[] termNumbersArray;
List<Object>termNumbers = new ArrayList<Object>();
. .
.
. .
.
then i attempt to add an element (numberOfTerms) to the list at position
'index' with the line :-

termNumbersArray[index].add(numberOfTerms);

It compiles correctly but gives a compiler warning "uses unchecked or unsafe
operations. Note:Recompile with -Xlint:unchecked for details."

How do I get around this?

Thanks

If your lists are lists of integers then why are you using Object as the
generic type argument?

List<Integer> termNumbers = new ArrayList<Integer>();

I am not 100% certain, but I have a feeling this will rid you of your
compilation warning.

cah.. of course... yes i'll try that.. i've been using Object because all of
the lists i've used so far have been needed to store lists of different data
types

i've just tried that but its still returning the warning...
 
W

Wesley Hall

Jeremy said:
Jeremy Watts said:
Wesley Hall said:
Jeremy Watts wrote:
i have created a one-dimensional array of type 'List' , ie. each of the
elements of the array is a list (of integers). The lines i used to do this
being :-

List[] termNumbersArray;
List<Object>termNumbers = new ArrayList<Object>();
. .
.
. .
.
then i attempt to add an element (numberOfTerms) to the list at position
'index' with the line :-

termNumbersArray[index].add(numberOfTerms);

It compiles correctly but gives a compiler warning "uses unchecked or unsafe
operations. Note:Recompile with -Xlint:unchecked for details."

How do I get around this?

Thanks


If your lists are lists of integers then why are you using Object as the
generic type argument?

List<Integer> termNumbers = new ArrayList<Integer>();

I am not 100% certain, but I have a feeling this will rid you of your
compilation warning.
cah.. of course... yes i'll try that.. i've been using Object because all of
the lists i've used so far have been needed to store lists of different data
types

i've just tried that but its still returning the warning...

OK, and what about if you change your array declaration to...

List<Integer>[] termNumbersArray;

Also, remember to include the generic type argument when you create the
array also, so termNumbersArray = new List<Integer>[size];
 
W

Wesley Hall

Wesley said:
Jeremy said:
Jeremy Watts said:
Jeremy Watts wrote:
i have created a one-dimensional array of type 'List' , ie. each of the
elements of the array is a list (of integers). The lines i used to do
this
being :-

List[] termNumbersArray;
List<Object>termNumbers = new ArrayList<Object>();
. .
.
. .
.
then i attempt to add an element (numberOfTerms) to the list at position
'index' with the line :-

termNumbersArray[index].add(numberOfTerms);

It compiles correctly but gives a compiler warning "uses unchecked or
unsafe
operations. Note:Recompile with -Xlint:unchecked for details."

How do I get around this?

Thanks


If your lists are lists of integers then why are you using Object as the
generic type argument?

List<Integer> termNumbers = new ArrayList<Integer>();

I am not 100% certain, but I have a feeling this will rid you of your
compilation warning.
cah.. of course... yes i'll try that.. i've been using Object because all of
the lists i've used so far have been needed to store lists of different data
types
i've just tried that but its still returning the warning...

OK, and what about if you change your array declaration to...

List<Integer>[] termNumbersArray;

Also, remember to include the generic type argument when you create the
array also, so termNumbersArray = new List<Integer>[size];

Oh you cant do that... 'generic array creation'. Hmmmm we are going a
little out of my depth with regards to generic arrays.

Anybody help us out here?
 
W

Wesley Hall

Wesley said:
Wesley said:
Jeremy said:
Jeremy Watts wrote:
i have created a one-dimensional array of type 'List' , ie. each of
the
elements of the array is a list (of integers). The lines i used to do
this
being :-

List[] termNumbersArray;
List<Object>termNumbers = new ArrayList<Object>();
. .
.
. .
.
then i attempt to add an element (numberOfTerms) to the list at
position
'index' with the line :-

termNumbersArray[index].add(numberOfTerms);

It compiles correctly but gives a compiler warning "uses unchecked or
unsafe
operations. Note:Recompile with -Xlint:unchecked for details."

How do I get around this?

Thanks


If your lists are lists of integers then why are you using Object as the
generic type argument?

List<Integer> termNumbers = new ArrayList<Integer>();

I am not 100% certain, but I have a feeling this will rid you of your
compilation warning.
cah.. of course... yes i'll try that.. i've been using Object because all
of
the lists i've used so far have been needed to store lists of different
data
types
i've just tried that but its still returning the warning...
OK, and what about if you change your array declaration to...

List<Integer>[] termNumbersArray;

Also, remember to include the generic type argument when you create the
array also, so termNumbersArray = new List<Integer>[size];

Oh you cant do that... 'generic array creation'. Hmmmm we are going a
little out of my depth with regards to generic arrays.

Anybody help us out here?

Seems there is lots of discussion on the creation of generic arrays in
Java, but the bottom line seems to be that you cant do it in any
reasonable way.

If you just want to clear the compiler warning, put the following
annotation on the method that does the add..

@SuppressWarnings({"unchecked"})
 
T

Thomas Hawtin

Wesley said:
Seems there is lots of discussion on the creation of generic arrays in
Java, but the bottom line seems to be that you cant do it in any
reasonable way.

Just don't use them. With generics there is little use for arrays of
reference types. Just use a List.
If you just want to clear the compiler warning, put the following
annotation on the method that does the add..

@SuppressWarnings({"unchecked"})

That is a measure of last resort. Not something to be thrown around just
to make the compiler shut up. If the compiler is making a noise, there
is probably a good reason.

Tom Hawtin
 
W

Wesley Hall

Thomas said:
Just don't use them. With generics there is little use for arrays of
reference types. Just use a List.

Fair point.
That is a measure of last resort. Not something to be thrown around just
to make the compiler shut up. If the compiler is making a noise, there
is probably a good reason.

In this case, it seems to be that the compiler is moaning that the OP is
using an array with no generic type, but as there seems to be no way to
create an array WITH a generic type (no reasonable way at least) then it
is not possible to avoid the compiler warning and still use arrays.
Using a list (as per you first point) is a reasonable measure but is not
always an option. There is still plenty of third party code that
requires the use of reference type arrays.

I don't agree that the above annotation is 'the measure of a last
resort'. A warning is exactly that, a warning. Once the developer has
analysed the problem, determining that the warning is valid but doesn't
pose any problem then using the annotation to suppress the warning is a
good move. It will prevent the next developer to compile the code from
wasting time analysing a superfluous warning.
 
J

Jeremy Watts

Thomas Hawtin said:
Just don't use them. With generics there is little use for arrays of
reference types. Just use a List.


That is a measure of last resort. Not something to be thrown around just
to make the compiler shut up. If the compiler is making a noise, there
is probably a good reason.

yeah thanks both. i got the 'generic array creation' error too... i would
side with trying something that doesnt involve compiler warnings rather than
just switiching off the error warning. it'd seem maybe that using perhaps a
'list of lists' may do it
 
T

Thomas Hawtin

Wesley said:
In this case, it seems to be that the compiler is moaning that the OP is
using an array with no generic type, but as there seems to be no way to
create an array WITH a generic type (no reasonable way at least) then it
is not possible to avoid the compiler warning and still use arrays.

Yes, creating an array with a generic type is BAD. Ditch the arrays.
Using a list (as per you first point) is a reasonable measure but is not
always an option. There is still plenty of third party code that
requires the use of reference type arrays.

But there is no need to let these creep into your code.
I don't agree that the above annotation is 'the measure of a last
resort'. A warning is exactly that, a warning. Once the developer has
analysed the problem, determining that the warning is valid but doesn't
pose any problem then using the annotation to suppress the warning is a
good move. It will prevent the next developer to compile the code from
wasting time analysing a superfluous warning.

In Java, a warning is essentially an error that cannot be treated as
such due to backward compatibility concerns. If you blindly suppress
notifications of errors, then you are storing up problems for future
developers. Even if no problem is (thought to be) posed, it is far
better to fix the cause rather than the effect.

Tom Hawtin
 
W

Wesley Hall

In Java, a warning is essentially an error that cannot be treated as
such due to backward compatibility concerns.

I don't agree with this definition at all.

A warning is the compiler informing the developer that some code, while
syntactically correct may exhibit unexpected behaviour at run time. It
is a pointer to take a closer look.

Warnings are not errors, and a majority have nothing to do with backward
compatibility.

If you blindly suppress
notifications of errors,

Doing anything 'blindly' is bad. I don't seem to recall suggesting blind
action.

Using the annotation for the purpose it was designed for is not acting
blindly and it is perfectly acceptable to resolve a warning by using
this annotation.

If you are saying that it shouldn't be used just to stop the warning
message without considering the meaning of the warning, then I agree. If
you are suggesting it should never be used (and there ARE situations
where the warning cannot be resolved in code), that I do not.
 
O

Oliver Wong

Wesley Hall said:
In this case, it seems to be that the compiler is moaning that the OP is
using an array with no generic type, but as there seems to be no way to
create an array WITH a generic type (no reasonable way at least) then it
is not possible to avoid the compiler warning and still use arrays.
Using a list (as per you first point) is a reasonable measure but is not
always an option. There is still plenty of third party code that
requires the use of reference type arrays.

I don't agree that the above annotation is 'the measure of a last
resort'. A warning is exactly that, a warning. Once the developer has
analysed the problem, determining that the warning is valid but doesn't
pose any problem then using the annotation to suppress the warning is a
good move. It will prevent the next developer to compile the code from
wasting time analysing a superfluous warning.

The problem with @SuppressWarning is that it's not fine-grained enough.
If I could apply it to a specific statement, or a specific expression, then
I'd have no problems with using it exactly as you've outlined (whenever the
developer analyzes the problem and determines the warning should be
suppressed, go ahead and use @SuppressWarning).

Unfortunately, @SuppressWarning only seems to apply at the method level
and field level. So if you apply @SuppressWarning to a method, you're
applying it to the whole method, and if you later on add or change the code,
you might be unintentionally suppressing new warnings you haven't had a
chance to look at yet.

This is why I usually "just tolerate" the unavoidable generic warnings.
When the number of warnings reported changes, I know it means there's
something new for me to look for.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top