Java Code using the List Interface not Compiling
-
Please consider the following Java fragment where there are two classes defined: Tire and Radial. The class Radial has Tire has its super class. That is, Radial extends Tire.
public static void addBrandTires(List parts1, List parts2, String brand) { ListIterator li = parts1.listIterator(); while ( li.hasNext() ) { Radial t1 = li.next(); if ( t1.getBrand().equals( brand) ) { parts2.add( t1 ); } } }
The call to add generates the following compiler error: error: no suitable method found for add(Radial) parts2.add( t1 ); Since t1 is of type Radial, it is of type Tire therefore I expected it to work. I tired putting in a cast of t1 but that did not work either. Bob
-
Please consider the following Java fragment where there are two classes defined: Tire and Radial. The class Radial has Tire has its super class. That is, Radial extends Tire.
public static void addBrandTires(List parts1, List parts2, String brand) { ListIterator li = parts1.listIterator(); while ( li.hasNext() ) { Radial t1 = li.next(); if ( t1.getBrand().equals( brand) ) { parts2.add( t1 ); } } }
The call to add generates the following compiler error: error: no suitable method found for add(Radial) parts2.add( t1 ); Since t1 is of type Radial, it is of type Tire therefore I expected it to work. I tired putting in a cast of t1 but that did not work either. Bob
It might be that you are missing the fact that Java requires you to pass an index for the element to be inserted at as well. Try this,
// Inserts at location 0.
parts2.add(0, t1);Also, if you need some other logic then implement that and change the "
0
" to a dynamic and controlled variable. [Java.util.ArrayList.add(int index, E elemen) Method Example](https://www.tutorialspoint.com/java/util/arraylist\_add\_index.htm) As to why you are facing this problem, read about the Capture Conversion, [What is a capture conversion in Java and can anyone give me examples? - Stack Overflow](https://stackoverflow.com/questions/4431702/what-is-a-capture-conversion-in-java-and-can-anyone-give-me-examples) [java - How can I add to List<? extends Number> data structures? - Stack Overflow](https://stackoverflow.com/questions/2776975/how-can-i-add-to-list-extends-number-data-structures) Lastly, it is much simpler to work withArrayList
in Java as compared to working with List
in Java.ArrayList
would be much better alternative to your types in the code above.ArrayList
containsList
in the inheritance chain. See the documentation for more on that. [ArrayList (Java Platform SE 8 )](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html)The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
Please consider the following Java fragment where there are two classes defined: Tire and Radial. The class Radial has Tire has its super class. That is, Radial extends Tire.
public static void addBrandTires(List parts1, List parts2, String brand) { ListIterator li = parts1.listIterator(); while ( li.hasNext() ) { Radial t1 = li.next(); if ( t1.getBrand().equals( brand) ) { parts2.add( t1 ); } } }
The call to add generates the following compiler error: error: no suitable method found for add(Radial) parts2.add( t1 ); Since t1 is of type Radial, it is of type Tire therefore I expected it to work. I tired putting in a cast of t1 but that did not work either. Bob
-
It might be that you are missing the fact that Java requires you to pass an index for the element to be inserted at as well. Try this,
// Inserts at location 0.
parts2.add(0, t1);Also, if you need some other logic then implement that and change the "
0
" to a dynamic and controlled variable. [Java.util.ArrayList.add(int index, E elemen) Method Example](https://www.tutorialspoint.com/java/util/arraylist\_add\_index.htm) As to why you are facing this problem, read about the Capture Conversion, [What is a capture conversion in Java and can anyone give me examples? - Stack Overflow](https://stackoverflow.com/questions/4431702/what-is-a-capture-conversion-in-java-and-can-anyone-give-me-examples) [java - How can I add to List<? extends Number> data structures? - Stack Overflow](https://stackoverflow.com/questions/2776975/how-can-i-add-to-list-extends-number-data-structures) Lastly, it is much simpler to work withArrayList
in Java as compared to working with List
in Java.ArrayList
would be much better alternative to your types in the code above.ArrayList
containsList
in the inheritance chain. See the documentation for more on that. [ArrayList (Java Platform SE 8 )](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html)The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
Missed that casting requirement. :sigh:
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
Missed that casting requirement. :sigh:
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
Please consider the following Java fragment where there are two classes defined: Tire and Radial. The class Radial has Tire has its super class. That is, Radial extends Tire.
public static void addBrandTires(List parts1, List parts2, String brand) { ListIterator li = parts1.listIterator(); while ( li.hasNext() ) { Radial t1 = li.next(); if ( t1.getBrand().equals( brand) ) { parts2.add( t1 ); } } }
The call to add generates the following compiler error: error: no suitable method found for add(Radial) parts2.add( t1 ); Since t1 is of type Radial, it is of type Tire therefore I expected it to work. I tired putting in a cast of t1 but that did not work either. Bob
Hey there, it seems like you're using the wildcard operator "?" and trying to use a method which is modifying the list at the same time. Now I am no expert but I think the above combination cannot be used since if you want to add something to a generic collection then as far as I remember you have to use the "super" keyword with the wildcard operator to let the compiler stop bothering you from adding modifying your collection and with the keyword super you'll have to change the declaration to one hierarchical level lower as well. Hope this helps, pardon me if I got it wrong though. Cheers.