diff --git a/core-java/src/main/java/com/baeldung/classcastexception/ClassCastException.java b/core-java/src/main/java/com/baeldung/classcastexception/ClassCastException.java index 754713dadb..64d56e6438 100644 --- a/core-java/src/main/java/com/baeldung/classcastexception/ClassCastException.java +++ b/core-java/src/main/java/com/baeldung/classcastexception/ClassCastException.java @@ -8,32 +8,16 @@ public class ClassCastException { public static void main(String[] args) { - List> personList = new ArrayList>(); + List> strList = new ArrayList>(); // To correct the Exception at line #18, modify the line #11 code as: // List> personList = new ArrayList >(); // Line #18 code as personList.add(Arrays.asList(personArray)); - Person p1 = new Person(1, "John"); - Person p2 = new Person(2, "Snow"); - Person[] personArray = new Person[] { p1, p2 }; - personList.add((ArrayList) Arrays.asList(personArray)); - System.out.println("Personlist: " + personList); + String p1 = new String("John"); + String p2 = new String("Snow"); + String[] strArray = new String[] { p1, p2 }; + strList.add((ArrayList) Arrays.asList(strArray)); + System.out.println("String list: " + strList); } } - -class Person { - int id; - String name; - - Person(int id, String name) { - this.id = id; - this.name = name; - } - - @Override - public String toString() { - return "Person [id=" + id + ", name=" + name + "]"; - } - -}