0

Java: I have implemented my own version of a Binary Heap. It should be able to store any type of Comparable object. The objects that are inserted into the heap come from input data, and all the input data will be of the same type. Is there a way to tell what Object type a given input is? I'm using

BufferedReader in = new BufferedReader(new InputStreamReader(System.in))

to read the inputs, and in.readLine() always returns a String. Right now, I'm explicitly testing to see if the user inputs an Integer, otherwise the object is always stored as a String.

What is the best way to read an input, see what type it is and then create a BinaryHeap<T> of that type, to then insert properly?

Purushotham
  • 3,770
  • 29
  • 41
  • Any Object can be converted to a String, but a reverse method is not available (or possible). You will have to add "explicitly testing" for all types you expect as input yourself, like you did for Integer. – vanOekel Feb 19 '14 at 12:15

1 Answers1

0
public class BinaryHeap<T extends Comparable<? super T>>
MayTheSchwartzBeWithYou
  • 1,181
  • 1
  • 16
  • 32