Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Java Code using the List Interface not Compiling

Java Code using the List Interface not Compiling

Scheduled Pinned Locked Moved Java
javahelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BobInNJ
    wrote on last edited by
    #1

    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

    A L V 3 Replies Last reply
    0
    • B BobInNJ

      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

      A Offline
      A Offline
      Afzaal Ahmad Zeeshan
      wrote on last edited by
      #2

      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 with ArrayList in Java as compared to working with List in Java. ArrayList would be much better alternative to your types in the code above. ArrayList contains List 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 !~

      L 1 Reply Last reply
      0
      • B BobInNJ

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You cannot add a Radial object to a List of Tires, only a Tire object. If you were allowed to then you could start trying to call Radial methods, or access Radial properties on Tire objects, which is not possible.

        1 Reply Last reply
        0
        • A Afzaal Ahmad Zeeshan

          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 with ArrayList in Java as compared to working with List in Java. ArrayList would be much better alternative to your types in the code above. ArrayList contains List 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 !~

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          List (Java Platform SE 7 )[^].

          A 1 Reply Last reply
          0
          • L Lost User

            List (Java Platform SE 7 )[^].

            A Offline
            A Offline
            Afzaal Ahmad Zeeshan
            wrote on last edited by
            #5

            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 !~

            L 1 Reply Last reply
            0
            • A Afzaal Ahmad Zeeshan

              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 !~

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Probably the first time I've seen you make a mistake.

              1 Reply Last reply
              0
              • B BobInNJ

                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

                V Offline
                V Offline
                vishaljamdagni
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups