generics

Solve “unchecked warning” in Java avoiding @suppressWarnings

冷暖自知 提交于 2020-01-24 13:03:08
问题 This is a Trying to improve my code question. About generics and the unchecked warning; I do know about the @suppresswarnings annotation. The question is what am I suppose to code to suppress it . Take, please, the following code. public class NumericBox<T extends Number> implements Box<T> { private T element; public NumericBox(T element) { this.element = element; } @Override public T getElement() { return element; } public T insert(Number newElement) { T oldElement = this.element; this

What's a C++ equivalent of Java wildcards?

我只是一个虾纸丫 提交于 2020-01-24 12:33:08
问题 In Java if you have a class like: class Box<E> { some code } you can do the following using a wildcard: Box<?> someBox; someBox = new Box<Integer>(); someBox = new Box<Double>(); Is there a way to do this in C++? In better words, how can I declare a variable in C++ that can hold either Box<Integer> or Box<Double> or Box<WhateverDataTypeHere> ? 回答1: template <typename T> class Box should inherit from a non-template base (let's say class BasicBox ). Then a pointer to BasicBox can point to

What's a C++ equivalent of Java wildcards?

做~自己de王妃 提交于 2020-01-24 12:32:45
问题 In Java if you have a class like: class Box<E> { some code } you can do the following using a wildcard: Box<?> someBox; someBox = new Box<Integer>(); someBox = new Box<Double>(); Is there a way to do this in C++? In better words, how can I declare a variable in C++ that can hold either Box<Integer> or Box<Double> or Box<WhateverDataTypeHere> ? 回答1: template <typename T> class Box should inherit from a non-template base (let's say class BasicBox ). Then a pointer to BasicBox can point to

Multiple Dispatch with Generics

大城市里の小女人 提交于 2020-01-24 12:25:05
问题 I'm trying to abstract away my interface implementations by providing a factory/builder using generics. However, I'm running into an issue with multiple dispatch and C# generics at run time that's doing something that seems odd. The basic scenario is I've defined several interfaces: public interface IAddressModel { } public interface IUserModel { } Then I have a factory class to return the actual implementations: public class Factory { public T BuildModel<T>() { return BuildModel(default(T));

InstantiationException while instantiating inner class using reflection. Why?

不羁岁月 提交于 2020-01-24 11:07:36
问题 i cant create B-Object, but why? public class AFactory { public int currentRange; private abstract class A { protected final Object range = currentRange; public int congreteRange = 28; } public class B extends A { public int congreteRange = 42; } synchronized A createNew(Class<? extends A> clazz) throws Exception { // EDIT: there is accessible default constructor currentRange = clazz.newInstance().congreteRange; return clazz.newInstance(); } public static void main(String[] args) throws

Using equals inside a generic class

我的未来我决定 提交于 2020-01-24 09:55:06
问题 I'd like my EqualTester generic class to call the overridden equals(...) method of its generic parameter, but it seems to call Object.equals instead. Here is my test code: import junit.framework.TestCase; public class EqualityInsideGenerics extends TestCase { public static class EqualTester<V> { public boolean check(V v1, V v2) { return v1.equals(v2); } } public static class K { private int i; private Object o; public K(Object o, int i) { this.o = o; this.i = i; } public boolean equals(K k) {

Inconsistent behavior in C# 8 nullable reference type handling with generics

て烟熏妆下的殇ゞ 提交于 2020-01-24 07:57:11
问题 I have this code: public T? Foo<T>() where T : class? { return null; } It gives an error that is logical and expected: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint. Now I add one more constraint: public T? Foo<T>() where T : class?, IDisposable // Could be any interface I guess { return null; } Now interestingly enough error has just disappeared. Though it really seems to me we have

Java compiler not able to infer type on generic chain

≯℡__Kan透↙ 提交于 2020-01-24 07:57:05
问题 As mentioned below (to remark it as I might have explained myself badly): I want to understand the principle behind this issue so that I can apply that knowledge to the real problem. ISSUE START I am working in a system intended to be an abstract library to be used by many subsystems. The idea is to have an standard behaviour extensible by implementations. My problem is that java compiler is not able to inferr method parameter type, even though it makes no sense since the boundaries are well

Generic SortedList, How to find the index of the first element that is greater than the search key?

不想你离开。 提交于 2020-01-24 05:46:05
问题 Without using extensions methods (LINQ). I am restricted to .NET 2.0 unfortunately. (Yeah, it sucks) Looking for something close to O(log(n)). Thanks for your help. 回答1: To find the first key that is greater than a given key you could use the list of keys SortedList<T>.Keys and perform a Binary Search or Interpolation Search on the keys. This will yield O(log(n)) (MSDN states that a key look up is O(1) ). 回答2: Binary search it for an O(n log n) lookup. 回答3: search where things are in oder O

Java infering wrong type of a typed HashSet [duplicate]

好久不见. 提交于 2020-01-24 05:23:50
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: ClassCastException when calling TreeSet<Long>.contains( Long.valueOf( someLongValue ) ) Please see the screenshot for the problem: It seems an entry in the Typed Set cylinderIds is suddenly of type String - but how did that happen? The bean is used from within a JSF page, but I always thought that the type system in Java should prevent this ... Any idea what is going wrong here? Using 1.7.0_06 64 Bit on Windows