Hello all I am using Generics in Java to perform some arithmetic operations, but I am getting some error **The operator + is undefined for the argument type(s) T, T** here is my code package com.genericsexample; public class GenericsArithmetic<T> { private T number1, number2,sum,product,difference; public void AddNumbers(T number1, T number2){ this.number1 = number1; this.number2 = number2; } public T getSum(){ // sum = number1 + number2; // when I uncomment the above line I am getting this error message.The operator + is undefined for the argument type(s) T, T return sum; } public void MultNumbers (T number1, T number2){ this.number1 …

Member Avatar
Member Avatar
+0 forum 16

hi,I have a classB which is in location "test\folder1\generics1" and Class2 which is generic class in another folder for eg test\folder2\ and when i create object for class2 which should be able to acces class1 method which is MethodA() please advise.Thanks in advance namespace generics1 { public class B { public void methodA() { } } public class A<T> { public void methodB() { base.MemberwiseClone(); } } class Program { static void Main(string[] args) { A<B> obj = new A<B>(); } } }

Member Avatar
Member Avatar
+0 forum 1

public class SongIterator<T,L> implements Iterator<Song<T, L>> {}

Member Avatar
Member Avatar
+0 forum 4

Suppose I have the following generic classes; Queue<T> KellysCoffee CheckOut and the following generic interfaces QueueInterface OrderLineInterface Suppose I want `KellysCoffee` to extend Queue class and implement `OrderLineInterface` This is how I've done it private class KellyCoffee<Order extends Queue<Order>> implements OrderLineInterface{} Now when I try to implement `CheckOut` public class CheckOut<Order> extends KellysCoffee<Order>{} I get a bound mismatch error saying The type Order is not a vlaid substitute for the bounded parameter <Order extends Queue<Order>> of the type KellysOrder<Order> when I try to extend `KellyCoffee` Is there a way to fix this?

Member Avatar
Member Avatar
+0 forum 4

would you just do @Override public boolean contains(T param){ return hashTable[function(param)]!=null; }

Member Avatar
Member Avatar
+0 forum 1

I'm having trouble pinpointing the exact definition of a generic linkedstack class Suppose I have public class LinkedStack<T> { //some variables //would this be right LinkedStack(){ } //for a generic constructor of type T for the LinkedStack class of type T? }

Member Avatar
Member Avatar
+0 forum 1

Hi everyone, we were taught about Generics and Interfaces at uni and I wasn't sure what the perfect situations are to use interfaces because they don't have implementation in them so they work as a contract to other classes where everyone can implement their methods as long as they extend the interfaces. As for generics, as far as I understand them, you make your own data structure or data type, right? that would be greatly appreciated if someone can clarify those points.

Member Avatar
Member Avatar
+0 forum 2

public class GenericDemo<E extends GenericDemo> { E innerE; public E doStuff(E e, GenericDemo<E> e2) { //insert code here } public E getE() { return innerE; } } The options are: 1. return e 2. return e2.getE() 3. return e2 4. return e.getE() Option (1) and (2) compile fine. Option (3) and (4) give compile error. 1. Precisely, for `e2` says, **incompatible types. Found: GenericDemo<E>. Required: E.** My doubts is that, for e2, isn't E that is required, part of GenericDemo<E> which is e2's type ? 2. For `return e.getE()` says, **incompatible types. Found: GenericDemo. Required: E.** Here I'm first confused …

Member Avatar
Member Avatar
+0 forum 8

import java.util.*; public class GenericDemo4 { public static void main(String r[]) { Set s1 = new HashSet(); s1.add(0); s1.add("1"); dostuff(s1); } static void dostuff(Set<Number> s) { do2(s); Iterator i = s.iterator(); while(i.hasNext()) System.out.println(i.next() + " "); Object [] oa = s.toArray(); for(int x = 0; x<oa.length; x++) System.out.println(oa[x] + " "); System.out.println(s.contains(1)); } static void do2(Set s2) { System.out.println(s2.size() + " "); } } On line 10 there is a type-safe set as a parameter for the method dostuff and it accepts non generic Set s1. How can it not flag an error for one of it's element being a …

Member Avatar
Member Avatar
+0 forum 7

import java.util.*; class SampleA { } class SampleB extends SampleA{ } class SampleC extends SampleA{ } class VectorDemo { public static void main(String r[]) { Vector<SampleA> v = new Vector<SampleA>(); v.add(new SampleB()); v.add(new SampleC()); SampleC rect = v.get(2); } } The output says "Incompatible types. Found SampleA, required SampleC. SampleC rect = v.get(2);" My doubt is first of all isn't this like using Wildcard where instead of saying Vector<? extends SampleA> v, we mentioned it a form of Vector<Type> v, which is one of the rules of Wild card which says if only <Type> is mentioned then it can accept …

Member Avatar
Member Avatar
+0 forum 3

Hello all, been a while since I've asked a question. I'm working on a project just to practice and for fun and I've run into a snag. I'm working on a painting type program and I'm trying to get a generic Buffer class for the images so I can use it for single float values like an alpha channel, or for a whole java.awt.Color. I'm setting up an interface for what the buffers will hold so that in any case I could call `buffer.setPixel(x,y,buffer.getPixel(x,y).add(new ColorValue());` This is the interface I would like for BufferValue<T>: /* * Copyright © 2012 William …

Member Avatar
Member Avatar
+0 forum 6

Related to: C#; Windows form, Storing Multiple Instances of a User Control: Hi: I’ve created an app with a user control that accepts parameters. When a user clicks a button I need to store the current view of the control and bring into focus on demand. Several instances of the UC need to be stored with different parms. Is a generic collection the best way to do this? Thanks.

Member Avatar
Member Avatar
+0 forum 4

Hellow. I have 3 variables 1. string query; 2. public static OracleCommand oracmd; 3. public static OracleDataReader reader; I have used these variables in a function [CODE=C#] . . . public void PreviousValue() { query = " select * from....... "; oracmd = new OracleCommand(); .... .... reader = oracmd.ExecuteReader(); } . . [/CODE] But I want to put these three variables in [B]C# Generics[/B] and then access those in [B]PreviousValue()[/B]. Can any one help me how to do that?

Member Avatar
Member Avatar
+0 forum 4

The sort methods for collections specifies that the parameter must be a collection of objects that implement Comparable for their own class or superclass, eg [CODE]public static <T extends Comparable<? super T>> void sort(List<T> list)[/CODE] Can anyone tell me how to define an ArrayList that will only accept such objects? I want to say [CODE]ArrayList<T extends Comparable<? super T>> eg = new ArrayList<String>();[/CODE] But that doesn't compile. I could use [CODE]ArrayList<? extends Comparable<?>> ok = new ArrayList<String>();[/CODE] but that's a bit too general (although the exceptions would be weird anyway). I know I'm going to look silly when someone points …

Member Avatar
Member Avatar
+0 forum 1

I have a problem with this construction, can not find how to rewrite it properly. [CODE] private SqlParameter op <T> (T? t, string name) { SqlParameter sp = new SqlParameter(); sp.IsNullable = true; sp.ParameterName = name; sp.Value = t.HasValue ? t.Value : Convert.DBNull; return sp; } [/CODE]

Member Avatar
Member Avatar
+0 forum 5

I'm trying to create a class that reads in a set of numbers from a file, stores them in a generic array, and then sorts them using the generic bubbleSort method. I usually write in C++ and the switch from templates to generics hasn't been easy. I'm fairly certain my bubbleSort method is correct, but I'm not sure what exactly is causing the problem. Thanks in advance for the help. Here is the error message: Bound mismatch: The generic method bubbleSort(T[], int) of type MySort<T> is not applicable for the arguments (T[], int). The inferred type T is not a …

Member Avatar
Member Avatar
+0 forum 10

I can understand that the subtype covariance relation should not hold for parameterized types, but why should it hold for arrays ? One of the books (Khalid Moghul) said [QUOTE]This relation holds for arrays because the element type is available at runtime[/QUOTE] How does the element type being available at runtime have anything to do with that ?

Member Avatar
Member Avatar
+0 forum 10

Hello. It's been a while since I was on here. I'm writing a Java class with a generic type parameter. Among other things, it maintains an internal ArrayList of the same generic type as itself. In a perfect world, the code would go like this: [CODE=Java]import java.util.ArrayList; public class MyGenericClass<T> { private ArrayList<T> items; public MyGenericClass() { items = new ArrayList<T>(); } }[/CODE] Unfortunately, we do not live in a perfect world, and this doesn't work because of type erasure. The only solution I can think of is to instantiate a raw ArrayList. (This will never result in a ClassCastException; …

Member Avatar
Member Avatar
+0 forum 5

I'm creating a system that has a parameterized class "TypeModule<T>". Now, I have another class that has the following member; [CODE]private LinkedHashMap<Class, ArrayList<TypeModule>> m_modules = ...[/CODE] Here's my dilemma. I need something more like the following; [CODE]private LinkedHashMap<Class<?>, ArrayList<TypeModule<?>>> m_modules = ...[/CODE] The only thing is, I need the '?'s to match types. I can't use a 'T' in place of the '?' because there's going to be different 'T's in the same map. I just want to tell the compiler that the '?'s are going to match. For example, this might be the contents of the map in a …

Member Avatar
Member Avatar
+0 forum 1

Hi all, to simplify my problem, I'll use a quick example. Imagine I have a class of 'Cat' which has accessors for things like 'name', 'colour' and 'height'. I then put these cats in a list [CODE]List<Cat^>^ catList;[/CODE] I want to be able to search this list based on the properties of the cats. e.g. "Find me the cat called 'Ginger'". I wish to use the List<T>::Find Method to perform this search. [URL="http://msdn.microsoft.com/en-us/library/x0b5b5bc(v=VS.90).aspx"]http://msdn.microsoft.com/en-us/library/x0b5b5bc(v=VS.90).aspx[/URL] Something along the lines of this c# line, but in c++.net [CODE]String catName = "Ginger"; Cat myLocatedCat = catList.Find(delegate(Cat c) {return c.Name == catName; });[/CODE] I tried …

Member Avatar
Member Avatar
+0 forum 1

Hi, I am working on assignments that deal with Generics in java. I have a class Movie that have a String name variable. However I have to add a few more properties to Movie objects like Release year and Director name. So I created an Inventory class that uses the Movie class objects. Then I created a sub-class ExtendedMovie within the Inventory class which needs a Movie object (string which is a movie name), and it also need a int year and String DirectorName. Now I am using ArrayList<ExtendedMovie> to build the inventory. My question is, how do I access …

Member Avatar
Member Avatar
+0 forum 1

I am getting the message when i start the computer that Generic Host Process for Win32 Services Error. I have seen the advanced options/details of the message and it is as under: szAppName : svchost.exe szAppVer : 5.1.2600.2055 szModName : wiaservc.dll szModVer : 5.1.2600.2055 offset : 00017f81 Please help me out of this. I am getting this message every time at the starting of the windows and afterwords..

Member Avatar
Member Avatar
+0 forum 2

I got yesterday a question how to get average date from list of dates, and here's my answer.

Member Avatar
+1 forum 0

The End.