generics

How does java generics syntax help avoid type casting?

▼魔方 西西 提交于 2020-01-30 13:20:26
问题 Below is the code, import java.util.List; import java.util.ArrayList; public class Dummy { public static void main(String[] args) { List<String> lst = new ArrayList<String>(); lst.add("a string"); lst.add("another string"); String s = lst.get(0); } //end main } when the constructor new ArrayList<String>(); is invoked, array of type Object is created. .. lst holds Object[0] array. So, If array of type Object gets created by constructor, How does javac does not see type casting issue in this

Java: Array of List<MyClass>

末鹿安然 提交于 2020-01-30 13:15:28
问题 I have a spec that requires me to pass an array of lists. The array is always length 2. I am using the following to accomplish this: List<MyClass> [] data = new ArrayList[2]; data[0] = new ArrayList<MyClass>(); data[1] = new ArrayList<MyClass>(); compiles but gives warning: uses unchecked or unsafe operations. I understand that Arrays of generics are not allowed in Java however I cannot change the spec and the above code seems to work nicely. As long as I am conscious that I never reassign

inheritance generic form cannot be shown in designer

不打扰是莪最后的温柔 提交于 2020-01-30 08:43:25
问题 I meet a problem today. As following. I create a generic Form , public class Form1:Form Then I create another inheritance form, public class From2:Form1. The form2 cannot be shown in the VS designer, the error message is "all the classes in the file cannot be designed", (this error message is translated from Chinese, the Chinese message is 文件中的类都不能进行设计). But this program can be compiled successfully, and when it runs, both Form1 and From2 can work. Anyone can give me some help about this ?

Type inference with existential type

♀尐吖头ヾ 提交于 2020-01-30 07:58:31
问题 I have a generic trait SomeTrait defined as so: trait SomeTrait[T] { def foo(t: T): String } And methods bar and qux as so: def bar[T](t: SomeTrait[T]): T def qux: List[SomeTrait[_]] I do not have control over the above. I am trying to operate on the list returned by qux , like so qux map { x => x.foo(bar(x))} However, the compiler complains that the types don't match up. As far as I know this should be fine. I have tried adding a generic method (signature [T](SomeTrait[T])String ), and

Calling static member when you have only generic parameter

折月煮酒 提交于 2020-01-30 07:39:48
问题 Ts there any way to call a static member on type when I only have a generic parameter. For example if I have something like this public Get<T>(int id) { // I would like to do this string s = T.SomeMethodName(); } I could do it like this but is kinda "yucky", and then it does not matter if it is static or not. Or I could use reflection as suggested by Yuriy. ISomeKnownInterface i = (ISomeKnownInterface ) new T(); string s = i.SomeMethodName(); So question is now which is better approach,

Calling static member when you have only generic parameter

僤鯓⒐⒋嵵緔 提交于 2020-01-30 07:39:05
问题 Ts there any way to call a static member on type when I only have a generic parameter. For example if I have something like this public Get<T>(int id) { // I would like to do this string s = T.SomeMethodName(); } I could do it like this but is kinda "yucky", and then it does not matter if it is static or not. Or I could use reflection as suggested by Yuriy. ISomeKnownInterface i = (ISomeKnownInterface ) new T(); string s = i.SomeMethodName(); So question is now which is better approach,

What's the correct way of retrieving my custom enumeration classes by their value?

我与影子孤独终老i 提交于 2020-01-30 07:08:09
问题 I've created my own custom pseudo enumerations within my domain model to allow me to have some more verbose values. For example, my class is as follows: public abstract class Enumeration<X, Y> : IComparable where X : IComparable { public Enumeration(X value, Y displayName) { } public Y DisplayName { get { return _displayName; } } public X Value { get { return _value; } } } And a class that inherits it would be: public class JobType : Enumeration<string, string> { public static JobType

What's the correct way of retrieving my custom enumeration classes by their value?

最后都变了- 提交于 2020-01-30 07:07:32
问题 I've created my own custom pseudo enumerations within my domain model to allow me to have some more verbose values. For example, my class is as follows: public abstract class Enumeration<X, Y> : IComparable where X : IComparable { public Enumeration(X value, Y displayName) { } public Y DisplayName { get { return _displayName; } } public X Value { get { return _value; } } } And a class that inherits it would be: public class JobType : Enumeration<string, string> { public static JobType

What's the correct way of retrieving my custom enumeration classes by their value?

不问归期 提交于 2020-01-30 07:07:10
问题 I've created my own custom pseudo enumerations within my domain model to allow me to have some more verbose values. For example, my class is as follows: public abstract class Enumeration<X, Y> : IComparable where X : IComparable { public Enumeration(X value, Y displayName) { } public Y DisplayName { get { return _displayName; } } public X Value { get { return _value; } } } And a class that inherits it would be: public class JobType : Enumeration<string, string> { public static JobType

What's the correct way of retrieving my custom enumeration classes by their value?

半腔热情 提交于 2020-01-30 07:07:10
问题 I've created my own custom pseudo enumerations within my domain model to allow me to have some more verbose values. For example, my class is as follows: public abstract class Enumeration<X, Y> : IComparable where X : IComparable { public Enumeration(X value, Y displayName) { } public Y DisplayName { get { return _displayName; } } public X Value { get { return _value; } } } And a class that inherits it would be: public class JobType : Enumeration<string, string> { public static JobType