473,585 Members | 2,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic generics help

I'm trying to write a data class using generics. This class is a Sparse
Matrix meaning that it can have many dimensions but not many entries (e.g. a
100 by 100 matrix, but there might only be 10 entries in the matrix).

I intend to use LinkedLists or Trees to manage the row and column keys and
probably a hash Map to manage the cells - but that's not important right
now.

My first step is that in my add method, I need to ensure that my row and
column keys are in my list. I would like to use one common routine for this.
The problem is I can't figure out the right generics syntax.

Here is what I started with:

public class SparseMatrix<R, C, E{

private TreeSet<RrowHea ders = new TreeSet<R();
private TreeSet<CcolHea ders = new TreeSet<C();

public void add (R rowKey, C colKey, E element) {

ensureExists (rowHeaders, rowKey);
ensureExists (colHeaders, colKey);
}

private void ensureExists (TreeSet<? extends ObjecttreeSet, Object
key) {
Iterator<? extends ObjectkeyIt = treeSet.iterato r();
Object k = null;
while (keyIt.hasNext( )) {
k = keyIt.next();
if (k.equals(key)) {
break;
}
k = null;
}
if (k == null) {
treeSet.add (key);
}
}
}
The problem is that the compiler is complaining about the treeSet.add (key)
near the bottom. The error is:
symbol : method add(java.lang.O bject)
location: class java.util.TreeS et<capture#346 of ? extends java.lang.Objec t>
treeSet.add (key);

So far I've searched google for the error message and tried various (random)
combinations of ensureExists method signatures, but just can't get it to
work. The only thing I've found to work is if I duplicate the routine and
overload it as in:
private void ensureExists (TreeSet<RtreeS et, R key) {
....
private void ensureExists (TreeSet<CtreeS et, C key) {
....

Obviously I'd rather not duplicate the method - that seems silly.

Any help appreciated.

TIA

Aug 29 '08 #1
4 2066
wizard of oz wrote:
public class SparseMatrix<R, C, E{

private TreeSet<RrowHea ders = new TreeSet<R();
private TreeSet<CcolHea ders = new TreeSet<C();

public void add (R rowKey, C colKey, E element) {

ensureExists (rowHeaders, rowKey);
ensureExists (colHeaders, colKey);
}

private void ensureExists (TreeSet<? extends ObjecttreeSet, Object
The problem is that the compiler is complaining about the treeSet.add
Well, the problem is that "rowHeaders " and "colHeaders " contain types of
R and C respectively, and you're trying to add a type of Object.

If you want to ensure that some type R "rowKey" exists in rowHeaders, then

boolean exists = rowHeaders.cont ains( rowKey );

will do the whole thing for you. I think however you are confused as to
what a Set like TreeSet will actually do for you. You seem to be trying
to ensure that the /pair/ R, E exists, in which case you need a Map, not
a Set.

E tempElement;
if( (tempElement = rowMap.get( rowKey )) != null ) {
// the pair (rowKey, element) exist
}
You might even put rowKey and colKey together in one single object
though and just do look-ups on that. Faster and easier.
Aug 29 '08 #2
Thanks to Mark and Lucas for your replies.

You are both of course correct, in fact I simply add the new keys to the row
and column headers. Because they are sets, I don't get any dups.

However the question is still relevant. What if I want to have a single
method that can manipulate either one of the headers and accept a parameter
appropriate to the "type" of header (i.e. an R or C)?

The signature
private void ensureExists (TreeSet<? extends ObjecttreeSet, Object
key) {

Allows me to pass either type of header set (row or column), but I can't
pass the parameter, so my question is about how do I get something like
these to work:
private void someMethod (TreeSet<? extends ObjecttreeSet, <? extends
Objectkey) {
or
private void someMethod (TreeSet<? extends ObjecttreeSet, ? key) {
or
private void someMethod (TreeSet<XtreeS et, X key) {

None of the above worked for me.
Another variant is
private TreeSet<Xsubset (TreeSet<XtreeS et, X startKey, X endKey) {
Where X is either an R or C. and the subset returns a subset of values
between the start and end key values.

Surely this is possible!?!?!

"Mark Space" <ma*******@sbcg lobal.netwrote in message
news:g9******** **@registered.m otzarella.org.. .
wizard of oz wrote:
>public class SparseMatrix<R, C, E{

private TreeSet<RrowHea ders = new TreeSet<R();
private TreeSet<CcolHea ders = new TreeSet<C();

public void add (R rowKey, C colKey, E element) {

ensureExists (rowHeaders, rowKey);
ensureExists (colHeaders, colKey);
}

private void ensureExists (TreeSet<? extends ObjecttreeSet, Object
>The problem is that the compiler is complaining about the treeSet.add

Well, the problem is that "rowHeaders " and "colHeaders " contain types of R
and C respectively, and you're trying to add a type of Object.

If you want to ensure that some type R "rowKey" exists in rowHeaders, then

boolean exists = rowHeaders.cont ains( rowKey );

will do the whole thing for you. I think however you are confused as to
what a Set like TreeSet will actually do for you. You seem to be trying
to ensure that the /pair/ R, E exists, in which case you need a Map, not a
Set.

E tempElement;
if( (tempElement = rowMap.get( rowKey )) != null ) {
// the pair (rowKey, element) exist
}
You might even put rowKey and colKey together in one single object though
and just do look-ups on that. Faster and easier.
Aug 29 '08 #3
wizard of oz wrote:
Surely this is possible!?!?!
No it's not.

In general you may have to abandon generics or pass around type tokens.
Check out _Effective Java_ by Joshua Bloch for more information on
ways to actually use generics.

However for your specific case this will do pretty well:

private <Tboolean ensureExists( Set<Ts, T key ) {
return s.contains( key );
}

Your program does have serious logic errors, you should use the design
Lucas gave you. In particular, ensureExists() won't tell you that the
/pair/ R, C exists, only that someone has stored some R and C in the
structure previously.

Aug 29 '08 #4
Does anyone think Java generics are misnamed?

Suppose I have the following:

LinkedList myList;
LinkedList <Foo yourList;

Isn't "myList" *more* generic, since it can hold
any type of Object, and can hold a mixture of
Objects (that is, the Objects in myList can have
different types)? Isn't "yourList" *less* generic,
since it can only hold Objects of type <Foo?
Sep 9 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
3306
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even...
3
2753
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some dead ends. I'm posting this to get some feedback on wether I'm going in the right direction, and at the same time hopefully save others from...
9
12813
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that implements IQueue<Tfor all types T (so it can make use of queues of various object types internally). As useful as this is, it doesn't seem possible. The...
1
3421
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to it. Therefore I wanted to creat a class member which represents the pointer to the array. Unfortunately I get the folowring compile error for the...
13
3809
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The...
10
1922
by: Egghead | last post by:
Hi all, Can someone kindly enough point me to some situations that we shall or "must" use Generic Class? I can foresee the Generic Method is powerful, but I can not find a single situation that I will need the Generic Class, given there are so many other options. -- cheers,
10
2703
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another class that contains an object of type T) I have a third generic class: MyHandler<Twhich contains 2 Add methods and a generic method: Add(T obj)
6
4618
by: Venkatesh Bhupathi | last post by:
Hi All, I am trying to inherit the Generic IList<Tinterface to form the typed collection of objects of type T. Following is my sample code, Public Class Record { public string name; public string address;
8
3144
by: MMAS | last post by:
Hey everyone -- Curious about some strange behaviour I'm seeing that seems to be related to my lack of understanding on how generics work in C#. Here's some simplified code (sorry for strange formatting) to show what my issue is. I have a generic abstract class here: public abstract class MyGenericAbstractClass<Twhere T : MyInterface
11
2538
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to...
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8336
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.