1 Reply Latest reply on Mar 21, 2018 2:52 PM by Chrys C.

    What do I do wrong with ArrayList?

    Frits Kuijk

      Hi, I'm working with OCA Java Programmer 1 Study Guide (exam 1Z0-808) (Sybex).

      While working out the ArrayList examples I get  an error on the non-generic examples on pages 130, 131 in the book.

      When I create the program:

       

       

      import java.util.*;

       

      public class ArrayListProblem {

          public static void main( String[] args) {

              ArrayList list = new ArrayList();

              list.add("hawk");         // [hawk]

              list.add(Boolean.TRUE); // [hawk, true]

              System.out.println(list);     // [hawk, true]

          }

      }

       

       

      compiling with "javac ArrayListProblem.java" results in:

      ArrayListProblem.java:8: error: no suitable method found for add(Boolean)

                      list.add(Boolean.TRUE); // [hawk, true]

                          ^

          method Collection.add(String) is not applicable

            (argument mismatch; Boolean cannot be converted to String)

          method List.add(String) is not applicable

            (argument mismatch; Boolean cannot be converted to String)

          method AbstractCollection.add(String) is not applicable

            (argument mismatch; Boolean cannot be converted to String)

          method AbstractList.add(String) is not applicable

            (argument mismatch; Boolean cannot be converted to String)

          method ArrayList.add(String) is not applicable

            (argument mismatch; Boolean cannot be converted to String)

      Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output

      1 error

       

      recompile with "javac -Xdiags:verbose ArrayListProblem.java" results in:

      ArrayListProblem.java:8: error: no suitable method found for add(Boolean)

                      list.add(Boolean.TRUE); // [hawk, true]

                          ^

          method Collection.add(String) is not applicable

            (argument mismatch; Boolean cannot be converted to String)

          method List.add(String) is not applicable

            (argument mismatch; Boolean cannot be converted to String)

          method List.add(int,String) is not applicable

            (actual and formal argument lists differ in length)

          method AbstractCollection.add(String) is not applicable

            (argument mismatch; Boolean cannot be converted to String)

          method AbstractList.add(String) is not applicable

            (argument mismatch; Boolean cannot be converted to String)

          method AbstractList.add(int,String) is not applicable

            (actual and formal argument lists differ in length)

          method ArrayList.add(String) is not applicable

            (argument mismatch; Boolean cannot be converted to String)

          method ArrayList.add(int,String) is not applicable

            (actual and formal argument lists differ in length)

      1 error

       

      Question: What do I do wrong or are the examples just only applicable to earlier versions than Java SE8? I understand the error message and agree with it, but then what about the example?

        • Re: What do I do wrong with ArrayList?
          Chrys C.

          Hello Frits,

           

          Thank you for your post!

           

          The warning is indicating that there is a type declaration on ArrayList, which is clearly not the case. I tested this on a few online compilers, and some worked while others did not. You might try the following to see if the compiler is for some reason suggesting the primitive over the object wrapper, which should still be valid:

           

          Boolean.TRUE.booleanValue();

           

          I don't believe the Java version is the issue, but maybe something specific to the compiler. Which compiler are you using?

           

          A new stack overflow thread may be a good idea for this.

           

          Please let me know if you have any questions. Have a great day!

           

          - Chrys