0
List<PhoneBook> list = new ArrayList<PhoneBook>();
String str = "new PhoneBook("David","234567");";

How can I add (or convert) str to code and add it to the list?

I have searched all over the internet, and didn't find anything helpful.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
bara
  • 1
  • 1
  • 2

1 Answers1

0

To convert it to fit the arraylist you've given:

List<PhoneBook> list = new ArrayList<>();
PhoneBook not_a_str = new PhoneBook("David","234567");
list.add(not_a_str);

To add strings to a list of objects:

List<Object> list = new ArrayList<>();
String str = "new PhoneBook(\"David\",\"234567\")";
list.add(str);

To add strings to a list of strings (also objects):

List<String> list = new ArrayList<>();
String str = "new PhoneBook(\"David\",\"234567\")";
list.add(str);
list.add("Anything else");

Good luck with java

Joe
  • 1,316
  • 9
  • 17