2

I try to populate ArrayLists with random values. The Arraylists can contain either Strings, Integers, Doubles or Chars. Creating random values works fine, but my Problem is that I am not able to write my insertRandom method. It should insert a specific data type (like double, int, etc). I described below what I want my insertRandom method to do. I am getting the warning "The method insert(T) in the type buffer is not applicable for the arguments(int). I googled it but did not find out / did not understand how to typecast my values to T objects, or how to check (similar to what "instanceof" does), what data types my ArrayLists contain. I already read about class types that people used as parameters in their constructor, but I am not allowed to change the parameter list of my constructor.

public class Buffer <T> {

private ArrayList<T> list;

    // constructor
    public Buffer (int capacity) {
        this.list = new ArrayList<T>();
    }

    // insert items to buffer
    public void insert (T item) {
        list.add(item);
    }

    // insert random values
    public void insertRandom () {

        check if ArrayList is of type String / Integer / Double

        if (ArrayList is-of-type-String) {
           insert("ThisString");
        }
        if (ArrayList is-of-type-Double) {
            insert(3.4);
        }

}

Thanks and best greetings, Patrick

AtMakeIT
  • 85
  • 6
  • 1
    Without knowing your assignment description exactly, this sounds like a terrible assignment on generics if the intention really is to store _any_ object in the same list. That's then basically an `ArrayList` and generics aren't really useful for this case. – Mick Mnemonic Mar 23 '19 at 19:36
  • Possible duplicate of [Get generic type of class at runtime](https://stackoverflow.com/questions/3403909/get-generic-type-of-class-at-runtime) – Sean Van Gorder Mar 25 '19 at 16:56
  • It's impossible to get the runtime type of T inside a method of Buffer, unless you pass a Class instance to the constructor and store it in a field. Are you sure this is what the assignment requires? – Sean Van Gorder Mar 25 '19 at 16:59

3 Answers3

1

Say even if you used generics and T is a Double, there is no way you can add a String to it. Which means there is no point of checking the type of class.

Buffer<Double> bd = new Buffer(10);
bd.insert(22.9);
bd.insert("string"); // compiler error

I don't think you need generics here. All you need is a Buffer<Object>, because as per your requirement you need to add Strings, Integers, Doubles or Chars. So you don't need to even do a type check.

Something like:

Buffer<Object> buff = new Buffer<Object>(10);
buff.insert(22.9);
buff.insert("");
buff.insert(13);
Nicholas K
  • 14,118
  • 7
  • 25
  • 49
  • Hi Nicholas, unfortunately I have to use the class "public class Buffer { } " for my homework... and I try to end up with 3 different ArrayLists, one containing only integers, one containing characters only. So they should not be mixed up together – AtMakeIT Mar 23 '19 at 18:58
  • 1
    Then create your object like `Buffer bd = new Buffer(10);`. Now you can *add* anything to it without doing any *type* check. – Nicholas K Mar 23 '19 at 19:00
1

As an option you can provide a Class of an object to the constructor and check this class in insertRandom() method, something like:

public class Buffer <T> {

    private final Class<T> type;
    private ArrayList<T> list;

    // constructor
    public Buffer (int capacity, Class<T> type) {
        this.list = new ArrayList<T>();
        this.type = type;
    }

    // insert items to buffer
    public void insert (T item) {
        list.add(item);
    }

    // insert random values
    public void insertRandom () {

//      check if ArrayList is of type String / Integer / Double     
        if (type == String.class) {
           insert((T)"ThisString");
        }

        if (type == Double.class) {
            insert((T)Double.valueOf(3.4));
        }
    }
}
Pavel Smirnov
  • 4,293
  • 3
  • 14
  • 23
  • Hello Pavel, thanks for the answer! This is one of the solutions I saw online, but my problem is that I am not allowed to change the parameter list of my constructor. Is there any other way how i could find out the type without changing the constructor? – AtMakeIT Mar 23 '19 at 19:04
  • 1
    @AtMakeIT, I'm not quite sure about your requiremetns, but obviously you know the type of objects to store in the Buffer when you create the Buffer. So, probably, you want to move insertRandom() method out of the Buffer class and fill it based on the same condition as you created it. – Pavel Smirnov Mar 23 '19 at 19:08
0

First point is you cannot use primitives like int for ArrayLists. Instead you have to use the corresponding class. In this case Integer. Second point: I think you you can check which type the ArrayList contains by doing T.class.equals(Integer.class).

embie27
  • 96
  • 5
  • thanks for the answer! To your first point: I used the insert-method in the way above and I think it works because of autoboxing. Because I thought this could be problematic i already tried to cast my variables to (Integer) or (Double) too. to the second point: I just tried T.class.equals(Integer.class); but Eclipse tells me "illegal class literal for the type parameter T" – AtMakeIT Mar 23 '19 at 18:57