1

I have a problem with the initialization of a List . The Class of the Items isn't known at compile time - they could be int, float, string or custom classes.

So I tried this:

public class Sensordevice {
    private List<?> valueList;

    public void setValueList(List<?> valueList) {
        this.valueList = valueList;
    }   

    public void addValue(Object value) {
        if(valueList == null){
            valueList = getList(value.getClass());
        }
        valueList.add(value);
    }

    private <T> List<T> getList(Class<T> requiredType) {
        return new ArrayList<T>();
    }
}

But I get this Error at valueList.add(value) in the addValue Methode:

The method add(capture#4-of ?) in the type List is not applicable for the arguments (Object)


Update

Thanks a lot for your replies. This solution works for my.

public class Sensordevice<T> {
    private List<T> valueList;

    public void setValueList(List<T> valueList) {
        this.valueList = valueList;
    }   

    public void addValue(T value) {
        if(valueList == null){
            valueList = new ArrayList<T>();
        }
        valueList.add(value);
    }
}
4
  • There's no point creating a List<T> if it's declared as List<?>. Commented Apr 20, 2015 at 16:48
  • 2
    Why not just use a List<Object>? Commented Apr 20, 2015 at 16:52
  • I would rather advice you to create class Sensordevice<T> Commented Apr 20, 2015 at 17:15
  • Use List<Object>, or, given you will have something to do with the values, is there no common behaviour that could be design by an interface, (e.g. Sensorable), and just simply use this in List<Sensorable> and so on? I don't see the case for wildcard here. Commented Apr 20, 2015 at 17:18

4 Answers 4

1

This works for me. And by "works" I mean I don't get any errors. It doesn't seem to provide any functionality since there isn't any way to get the list of objects from the Sensordevice since getList just returns a new, empty list, but that's the code you gave. I think the core of the error is having addValue take Object instead of T.

public class Sensordevice {
    private List valueList;

    public <T> void setValueList(List<T> valueList) {
        this.valueList = valueList;
    }

    public <T> void addValue(T value) {
        if(valueList == null){
            valueList = getList(value.getClass());
        }
        valueList.add(value);
    }

    private <T> List<T> getList(Class<T> requiredType) {
        return new ArrayList<>();
    }
}

public static void main(String[] args) {
    Sensordevice sd = new Sensordevice();
    sd.addValue(new Object());
    sd.addValue(new Integer(3));
    sd.addValue("");
    sd.addValue(new Sensordevice());

    System.out.println(sd.getList(Sensordevice.class));
}
Sign up to request clarification or add additional context in comments.

Comments

1

So if you don't know particular type would you class use, make your class generic:

public class Sensordevice<T> { 

    private List<T> valueList;

    public void setValueList(List<T> valueList) {
        this.valueList = valueList;
    }   

    public void addValue(T value) {
        if(valueList == null){
            valueList = getList(value.getClass());
        }
        valueList.add(value);
    }

    private List<T> getList() {
        return new ArrayList<T>();
    }
}

3 Comments

I think that he really wants to deal with lists of different types, where the type does not vary from program to program, but during the actual computation.
getList(value.getClass()); private List<T> getList() is it working?
@AndreyTyukin then he needs List<Object> of just unparametrized List
0

If you don't know the List type you can leave it without any type specification, just put: private List valueList;

4 Comments

Is this allowed again? I thought they deprecated it since at least 1.5? Maybe they re-introduced it for some reason, I'm not sure. Somewhere between 1.5 and 1.6 one certainly would get compiler warnings for that.
@AndreyTyukin it is not recommended, BTW it is still widely used in Java SE sources. Some libs are full of @SupressWarnings("rawtypes")
@SashaSalauyou I assume those libs were grandfathered in. Are there libraries written in the past five years that use raw types?
Yeah, well... Maybe writing @SuppressWarnings("rawtypes") once is indeed shorter than adding <Object> everywhere, in certain circumstances...
0

Change the valueList to: private List valueList; and getList() to:

private <T> List<Object> getList(Class<T> requiredType) {
        return new ArrayList<Object>();
    }

This fixes the error and it appears to work properly. I tested it with strings, floats, and ints.

3 Comments

if List<Object> is always returned, what is purpose of requiredType at all?
It has to be list<Object> in order to satisfy 'private List<Object> valueList;' You can keep your getList the way it is and make 'valueList = (List<Object>) getList(value.getClass());' in 'addValue', but this will give you a safety check warning.
If so, simple getList() is enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.