BAEL-2024 : ClassCastException : Fixing review comments

This commit is contained in:
Kirti Deo 2018-08-20 21:12:40 +05:30
parent 8c874c0f7d
commit 8136464a6e
1 changed files with 6 additions and 22 deletions

View File

@ -8,32 +8,16 @@ public class ClassCastException {
public static void main(String[] args) { public static void main(String[] args) {
List<ArrayList<Person>> personList = new ArrayList<ArrayList<Person>>(); List<ArrayList<String>> strList = new ArrayList<ArrayList<String>>();
// To correct the Exception at line #18, modify the line #11 code as: // To correct the Exception at line #18, modify the line #11 code as:
// List<List<Person>> personList = new ArrayList <List<Person>>(); // List<List<Person>> personList = new ArrayList <List<Person>>();
// Line #18 code as personList.add(Arrays.asList(personArray)); // Line #18 code as personList.add(Arrays.asList(personArray));
Person p1 = new Person(1, "John"); String p1 = new String("John");
Person p2 = new Person(2, "Snow"); String p2 = new String("Snow");
Person[] personArray = new Person[] { p1, p2 }; String[] strArray = new String[] { p1, p2 };
personList.add((ArrayList<Person>) Arrays.asList(personArray)); strList.add((ArrayList<String>) Arrays.asList(strArray));
System.out.println("Personlist: " + personList); 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 + "]";
}
}