ArrayList<Object> list = new ArrayList<Object>();
list.add(1);
list.add("Java");
list.add(3.14);
System.out.println(list.toString());
I tried:
ArrayList<String> list2 = (String)list;
But it gave me a compile error.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since this is actually not a list of strings, the easiest way is to loop over it and convert each item into a new list of strings yourself:
Or when you’re not on Java 16 yet:
Or when you’re not on Java 8 yet:
Or when you’re not on Java 7 yet:
Note that you should be declaring against the interface (
java.util.Listin this case), not the implementation.