Click here to Skip to main content
15,946,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I complete this classification program. I would like to add element to corressponding array list.
public class Classification<T> {
    private ArrayList<T>integer = new ArrayList<T>();
    private ArrayList<T>Double = new ArrayList<T>();
    private ArrayList<T>string = new ArrayList<T>();
    private ArrayList<T>character = new ArrayList<T>();
    public void add(T t){
        if (t.toString()==t){

What I have tried:

I try to use
if ((double)t == t){

However, it gives me an error message which is operator == cannot be applied to T
Updated 2-Feb-17 20:51pm

1 solution

public class Classification<t> {
Since your class should classify passed data, it makes no sense making it dependent upon a generic parameter.

private ArrayList<t>integer = new ArrayList<t>();
private ArrayList<t>Double = new ArrayList<t>();
private ArrayList<t>string = new ArrayList<t>();
private ArrayList<t>character = new ArrayList<t>();

Why are you creating generic ArrayList when you know in advance they will hold just one type. For instance, why don't you use
private ArrayList<Integer>integer = new ArrayList<Integer>();

I would like to add element to corressponding array list.
Just write the corrensponding overloaded add method, e.g.
public void add(Integer t)
  // add to 'integer' ArrayList
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900