generics

casting Object[] to a reference type array in java

≯℡__Kan透↙ 提交于 2021-02-05 04:51:20
问题 In implementation of a generic stack ,the following idiom is used and works without any problem public class GenericStack<Item> { private int N; private Item[] data; public GenericStack(int sz) { super(); data = (Item[]) new Object[sz]; } ... } However when I try the following ,it causes a ClassCastException String[] stra = (String[]) new Object[4]; Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; How do you explain this? 回答1:

casting Object[] to a reference type array in java

半腔热情 提交于 2021-02-05 04:50:17
问题 In implementation of a generic stack ,the following idiom is used and works without any problem public class GenericStack<Item> { private int N; private Item[] data; public GenericStack(int sz) { super(); data = (Item[]) new Object[sz]; } ... } However when I try the following ,it causes a ClassCastException String[] stra = (String[]) new Object[4]; Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; How do you explain this? 回答1:

Understanding generics in Java

蓝咒 提交于 2021-02-04 21:56:51
问题 Can someone explain to me what this code is doing, I am new to java. Queue <TreeNode> queue = new LinkedList<TreeNode>(); I believe this code creates a Queue with the variable name queue that is of type TreeNode , what does the right side mean? Is it assigning the Queue to be a Linked List of type TreeNode ? 回答1: Class java.util.Queue uses type parameters to assure that you won't add type other than it was created with. If you create Queue like: Queue <TreeNode> queue = new LinkedList

Understanding generics in Java

限于喜欢 提交于 2021-02-04 21:55:06
问题 Can someone explain to me what this code is doing, I am new to java. Queue <TreeNode> queue = new LinkedList<TreeNode>(); I believe this code creates a Queue with the variable name queue that is of type TreeNode , what does the right side mean? Is it assigning the Queue to be a Linked List of type TreeNode ? 回答1: Class java.util.Queue uses type parameters to assure that you won't add type other than it was created with. If you create Queue like: Queue <TreeNode> queue = new LinkedList

Can we create an Array of Generic class? [duplicate]

北城余情 提交于 2021-02-04 21:09:21
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Java how to: Generic Array creation I want to create a Stack which contains 10 elements, I'm trying to do this with Generics. I started by creating a Stack class of Type T, but I'm getting compilation error when I try to instantiate the array of generic type. public class Stack<T>{ private T[] stk = new T[10]; } What I'm doing wrong here? 回答1: You can't do this. Not without using some hacky round about code, and

Can we create an Array of Generic class? [duplicate]

☆樱花仙子☆ 提交于 2021-02-04 21:08:15
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Java how to: Generic Array creation I want to create a Stack which contains 10 elements, I'm trying to do this with Generics. I started by creating a Stack class of Type T, but I'm getting compilation error when I try to instantiate the array of generic type. public class Stack<T>{ private T[] stk = new T[10]; } What I'm doing wrong here? 回答1: You can't do this. Not without using some hacky round about code, and

Typing an array of generic inferred types

情到浓时终转凉″ 提交于 2021-02-04 18:52:06
问题 I'm trying to create the type of an array of objects. The first and second key of this object is required to match. For example: [{ key1: "hi", key2: "world" },{ key1: 1, key2: 2 },{ key1: true, key2: false }] This is what I've come up with but it doesn't exactly work. I have a generic type to define the object in the array. When calling it to generate the array type, an error is raised. type ArrayItem<T> = { key1: T, key2: T } // This raises an error Generic Type ArrayItem requires 1 type

Passing a generic as generic type parameter in c#

帅比萌擦擦* 提交于 2021-02-04 18:49:09
问题 I did a little experiment on generics in C# and I had a problem where I want to pass a generic type as a type parameter with a constraint to implement a generic interface whose type I don't know. Here is my example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication3 { class Program { interface IGenericCollection<T> { IEnumerable<T> Items { get; set; } } abstract class GenericCollection<T> :

Open generic interface types of open implementation don't equal interface type?

故事扮演 提交于 2021-02-04 17:17:49
问题 Here's a test that should, in my opinion be passing but is not. [TestMethod] public void can_get_open_generic_interface_off_of_implementor() { typeof(OpenGenericWithOpenService<>).GetInterfaces().First() .ShouldEqual(typeof(IGenericService<>)); } public interface IGenericService<T> { } public class OpenGenericWithOpenService<T> : IGenericService<T> { } Why does this not pass? Given Type t = typeof(OpenGenericWithOpenService<>) how do I get typeof(IGenericService<>)? I'm generally curious, but

Restrict generic T to specific types

元气小坏坏 提交于 2021-02-04 15:41:35
问题 I have the following method: public static IResult<T, String> Validate<T>(this T value) { } // Validate How can I restrict T to be Int16, Int32, Int64, Double and String? 回答1: You can't restrict generics in that way, you can only choose a single class as a constraint. You must either make 5 overloads or find a interface all 5 of those things share and use that. Which option you choose will depend on what Validate doe. Here is how you would do the overloads. public static IResult<Int16, String