The original question I posted on Yahoo Answers:

http://answers.yahoo.com/question/index;_ylt=Au6uML15p1p500urP_yfL9fsy6IX;_ylv=3?qid=20111002100030AAILsNC


I can't get this code to return anything:

In NewVector.java: a class that extends Vector:

public Object getSmallest()
{
Object obj = elementData[0];

try
{
String a = (String)elementData[0];
for(int i=1;i<elementCount;i++)
{
String b = (String)elementData[i];
if(a.compareTo(b)>0)
{
a = b;
obj = elementData[i];
}
}
}
catch(NullPointerException e)
{
}
return obj;
}

in StringVector.java: a vector holding class that just holds objects that holds and does stuff with strings(StringElement):

public String getSmallest()
{
return strings.getSmallest().toString();
}

and the driver:

else if(num==8)
{
System.out.println(vect.getSmalle…
}

No idea whats going on. Any ideas?


I was curious, since I want NewVector to be able to work with any kind of data structure, be it char, double or some construct, would I be able to do something like:

Object a = something;
String b = a.toString();

What would be the best way of doing this?

Recommended Answers

All 5 Replies

First major mistake is this

catch(NullPointerException e)
{
}

Did you get an NPE when you ran your code? You don't know, and neither do we.
Before you do anything else fix this blunder by putting an e.printStackTrace() in the catch and run it again.

Either way - why are you trying to convert to String? You can't meaningfully compare arbitrary Objects by comparing their String representations - eg for Integers you will get a sort order like 1, 10, 11, 2, 3 etc, and for eg instances of the Random class it makes no sense whatsoever.
For meaningful results you should restrict your class to extend something like
Vector<? extends Comparable> so you know you can compare the objects directly

Finally, its a terrible idea to access Vector's protected implementation variables such as elementData. There is absolutely no guarantee that the next maintenance release of Java will not change those names, or even change the whole implementation. You can only rely on the public methods described in the JavaDoc.

First major mistake is this

catch(NullPointerException e)
{
}

Did you get an NPE when you ran your code? You don't know, and neither do we.
Before you do anything else fix this blunder by putting an e.printStackTrace() in the catch and run it again.

Either way - why are you trying to convert to String? You can't meaningfully compare arbitrary Objects by comparing their String representations - eg for Integers you will get a sort order like 1, 10, 11, 2, 3 etc, and for eg instances of the Random class it makes no sense whatsoever.
For meaningful results you should restrict your class to extend something like
Vector<? extends Comparable> so you know you can compare the objects directly

Finally, its a terrible idea to access Vector's protected implementation variables such as elementData. There is absolutely no guarantee that the next maintenance release of Java will not change those names, or even change the whole implementation. You can only rely on the public methods described in the JavaDoc.

Sorry for responding so late. I added printStackTrace. Nothing happened.

What I was supposed to do was compare an object that holds a string and does things with it. But I was supposed to make the class generic so if I used it with Integer for example it would work. I wouldn't have it compare two random objects like an instance of random. Is this possible?

I'm looking at the Comparable Interface on the Oracle website. So I need to overwrite the compareTo method to define what it means for objects of type T? If its supposed to be generic how do I do that? Maybe I misinterpreted what it is I had to do.

You can make it generic, ie able to work with any kind of object in the vector provided that the objects can be compared. If they can't be compared then the whole idea of this method doesn't apply to them. In Java you know objects can be compared because they implement the Comparable interface - which comprises the compareTo method. All the obvious classes such as String, Integer etc implement Comparable - you don't need to override compareTo because they already have it. Classes like Random don't, because it makes no sense.
In short, if you set up your generics right you can use compareTo directly on them to make the comparisons because you know whatever has been passed in has that method.

OK. I gave up on going the generic route and simply declared a Vector in StringVector and put getSmallest and getLargest in there. I'm curious though how you would've done it. It appears you know how to do it. I'm not too familiar with coding generically in java and as you can see I got burned so...

Some background info:

StringElement extends DataElement, an abstract class meant teach how to use an implement abstract classes and write generic code.
StringElement just holds a String and just does comparison and stuff like that.
NewVector you know
StringVector was the middle man that would've held an instance of NewVector to hold StringElements as well as some other stuff
driver.java does the interface with the user taking in commands and spitting output it gets from StringVector.

I can upload the code if you want

The goal is to declare a Vector whose elements can be compared - ie that implement Comparable. The simplest form of that is

Vector<? extends Comparable<?>>

you can use that generic form to specify (eg) a parameter passed to a getSmallest method, and you will be able to use compareTo on the Vector's elements without needing to know the exact class of the elements, and without casting anything.
Because Comparable interface is also generic, this particular example of generics comes under the "advanced" heading - it's not easy to understand or get right first time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.