0

Why i get a compilation error in this line String s=data.get(idx);

Update: I get Type mismatch: cannot convert from E to String

    public class Parent<E> {
        ArrayList<E> data=new ArrayList<E>();

        public void add(E d){
            data.add(d);
        }
        public List<E> getData(){
            return data;
        }
    }

    public class Child<E> extends Parent<E>{

        public void appendData(E newItem){
            super.add(newItem);
        }
        public void displayData(int idx){
            List<E> data=this.getData();
            **String s=data.get(idx);**//I get compilation error in this line
            System.out.println(s);
        }

        public static void main(String[] args) {
            Child<String> c=new Child<String>();
            c.appendData("Data1");
            c.appendData("Data2");

            c.displayData(1);
        }
    }

Solution Updated Class:

public class Child<S> extends Parent<String>{

public void appendData(String newItem){
    super.add(newItem);
}
public void displayData(int idx){
    List<String> data=this.getData();
    String s=data.get(idx);
    System.out.println(s);
}

public static void main(String[] args) {
    Child c=new Child();
    c.appendData("Data1");
    c.appendData("Data2");

    c.displayData(1);
}
}
8
  • What is the error you are getting? I assume you want to have toString() here data.get(idx);. Commented Aug 22, 2013 at 3:35
  • You have to cast like this: String s=(String)data.get(idx) otherwise you have to use List<String> data instead of List<E> data Commented Aug 22, 2013 at 3:43
  • Generics takes care of Type Casting right? Commented Aug 22, 2013 at 3:47
  • Child<String> c=new Child<String>(); I have declared like this, It also implies that type argument in data=new ArrayList<E>(); is String. Then why should i do a explicit type casting... Commented Aug 22, 2013 at 3:49
  • Should be E s = data.get(idx) and then s instanceof String would return true, therefore you could do String s = (String) data.get(idx); Commented Aug 22, 2013 at 3:51

6 Answers 6

3

It shouldn't be something hard to understand:

In String s=data.get(idx);, data is in type of ArrayList<E>. Given you have no special restriction in E, it is possible to be any type that is NOT a String. Just think, what will happen if you are using a Child<Date>, for which the data is in fact an ArrayList<Date>, String s = data.get(i) simply doesn't make sense.

Hence, compiler complain to you that you cannot simply assume the result of data.get(index) to be a String.

The way to fix can be straight-forward. For example you can do whatever suggested in other people's answer. It may also be an issue of your design that, your Child should not bear type parameter and it should extends from Parent<String>. However it is more important that you understand why it doesn't work, so that you can choose the correct way to fix your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Updated classpublic class Child<S> extends Parent<String>{ public void appendData(String newItem){ super.add(newItem); } public void displayData(int idx){ List<String> data=this.getData(); String s=data.get(idx); System.out.println(s); } public static void main(String[] args) { Child c=new Child(); c.appendData("Data1"); c.appendData("Data2"); c.displayData(1); } }
1

here you go.

data.get(idx).toString();

Comments

1

That line should read:

E s = data.get(idx);

Then if you want to print it, you can do it like this:

System.out.println(s.toString());

Comments

0

This is not really related to generics. But to answer the question,

String s=data.get(idx);

should be replaced with

String s= String.valueOf( data.get(idx) );

If you are absolutely sure that data.get(idx) will never return null, you can also use:

String s= data.get(idx).toString() ;

Otherwise, you will receive NullPointerExceptions.

On a second read, you can also use

System.out.println( data.get(idx) ) ;

directly. Internally, this uses String.valueOf() so it's equally exception-safe.

Comments

0

The reason why you get the compilation error is because in the Child class, where you call data.get(idx) It doesn't know that it's going to be a String.

public void displayData(int idx) {
    List<E> data = this.getData();
    //String s=data.get(idx); //Doesn't compile because it won't know this is a String at runtime
    E s = data.get(idx); // To make it generic to the type in main, change the type to E
    System.out.println(s);
}

Comments

0

You can solve the problem either casting it to string or use string in your generic parameter

String s = data.get(idx);
ArrayList<String> data=new ArrayList<String>();

Reason you are getting this error because you are using Generic methods and type parameter So when you are assigning value from your generic list to a string you are getting error. Compiler does not know that your List actually contains string data.

Comments

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.