Comp Sci What does the error T does not match T mean in my Java code?

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Array Java List
AI Thread Summary
The error "T does not match T" in Java typically arises from type mismatch issues, particularly when generics are involved. In the provided code, the declaration of `_underlyingStorage` as `private T[] _underlyingStorage = (T[]) new Object[128];` is problematic because it uses a raw type, leading to potential type safety violations. The compiler may not be able to infer the correct type for `T`, resulting in confusion during compilation. Additionally, providing specific line numbers and verbatim error messages can help others diagnose the issue more effectively. Ensuring proper use of generics and avoiding raw types can resolve this compilation error.
magnifik
Messages
350
Reaction score
0
I am getting compilation errors in the following segment of my code:

_underlyingStorage was declared in the following way: private T[] _underlyingStorage = (T[]) new Object[128];

public T next() {
_currentIndex++;
if(_underlyingStorage[_currentIndex] == _underlyingStorage[_numElements-1])
_underlyingStorage[_currentIndex] = _underlyingStorage[0];
return _underlyingStorage[_currentIndex];
}

the error I'm getting makes no sense to me.. it says that T does not match T. what's happening here?
 
Physics news on Phys.org
Where are you getting the error at? I entered everything you had into a class, but I'm not getting any error.
 
magnifik said:
the error I'm getting makes no sense to me.. it says that T does not match T. what's happening here?
Errors reported by the compiler and the runtime are usually very precisely worded, containing a very literal description of a problem in your program and where that problem occurs.

If you aren't yet experienced enough to extract this information from an error, you should copy it verbatim when you ask for others to help. (also, there's probably a line number involved. It would help if you point out which line of your code it refers to)
 

Similar threads

Replies
2
Views
1K
Replies
5
Views
3K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
12
Views
2K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
15
Views
2K
Replies
2
Views
4K
Back
Top