Java Generics - difficulty enforcing strong type checking?

The constructor in the inner for loop should have generics.

Up vote 1 down vote favorite share g+ share fb share tw.

Here's my code: public class Sequence { protected List sequence = new ArrayList(); public Matrix> createCartesianProduct(Sequence secondSequence) { Matrix> result = new Matrix>(); for (int rowIndex = 0; rowIndex > row = new Sequence>(); for (int columnIndex = 0; columnIndex should be parameterized. Type Safety: The expression of type OrderedPair needs unchecked conversion to conform to OrderedPair Type safety: The Constructor OrderedPair(Object, Object) belongs to the raw type OrderedPair. References to generic type OrderedPair should be parameterized I would like to use generics to enforce strong type-checking here, but I guess my understanding of generics is not sufficient to allow me to see how.

Could someone educate me? Thanks, -- Ken java generics strong-typing link|improve this question asked Dec 22 '08 at 19:22Ken.

Ugh. This is why refied generics should've been no. 1 in devoxx.

– Ran Biron Dec 22 '08 at 20:47.

The constructor in the inner for loop should have generics: row. Add(new OrderedPair(sequence. Get(rowIndex), secondSequence.sequence.

Get(columnIndex))); If the compiler complains about the "? " (I think it will, but it's not easy for me to test), then replace all the? 's with a letter, say E.

Then add an into the signature, like this: public Matrix> createCartesianProduct(Sequence secondSequence) { (otherwise, the compiler won't know where the E came from).

" (some unknown type). So the E is definitely needed. – newacct May 2 '09 at 6:06.

The OrderedPair is not generified but it's added to a list (Sequence) which is generified. You have to construct the OrderedPair with generics, e.g. Do "new OrderedPair(...)", to get rid of this warning. Here I have added generics to the whole method, so the return type matches the secondSequence's type: public Matrix> createCartesianProduct(Sequence secondSequence) { Matrix> result = new Matrix>(); for (int rowIndex = 0; rowIndex > row = new Sequence>(); for (int columnIndex = 0; columnIndex void addToRow(Sequence> seq, T t, Z z) { seq.

Add(new OrderedPair(t, z)); }.

I think that you are a bit confused here. In the type Sequence what will be the T? If you define a Sequence> then you end up with recursion on T.

Please see if what you really need is something like this: public class Sequence { protected List sequence = new ArrayList(); public Matrix> createCartesianProduct(Sequence secondSequence) { Matrix> result = new Matrix>(); for (int rowIndex = 0; rowIndex row = new Sequence(); for (int columnIndex = 0; columnIndex (sequence. Get(rowIndex), secondSequence.sequence. Get(columnIndex))); } } return result; } }.

Trust compiler and try to use generic parameters always when calling for OrderedPair :) It's not required but I think it's a good practice. Strict generic applying in Java is not possible because of this: Type erasure.

All you need to do is add a generic type to your constructor, like so: row. Add(new OrderedPair(sequence. Get(rowIndex), secondSequence.sequence.

Get(columnIndex))); The compiler is throwing errors since OrderedPair expects to receive types while you are passing them without any explicit type. The unchecked conversion the compiler is talking about is since basically you are giving the constructor while it wants hence the unchecked conversion that is going on, and which might throw an exception if the wrong type accidentally gets passed.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions