I'm trying to create a phone book using a 2D ArrayList based on user input. Here is my current code:
ArrayList<ArrayList<String> info = new ArrayList<ArrayList<String>>();
ArrayList<String> entry = new ArrayList<String>();
System.out.println("Enter first and last name: ");
entry.add(addScan.nextLine());
System.out.println("Enter Address: ");
entry.add(addScan.nextLine());
System.out.println("Add phone number: ");
entry.add(addScan.nextLine());
info.add(entry);
This is inside a do-while loop that exits when the user is done entering contacts. I printed with System.out.println(info);
Here is the output when I printed:
[[John Snow, 1234 King's Landing, 123-456-7890, Tyrion Lanister,
4567 Castle Black, 222-222-2222, Ned Stark, 999 Winterfell Lane,
777-777-7777], [John Snow, 1234 King's Landing, 123-456-7890,
Tyrion Lanister, 4567 Castle Black, 222-222-2222, Ned Stark, 999
Winterfell Lane, 777-777-7777], [John Snow, 1234 King's Landing,
123-456-7890, Tyrion Lanister, 4567 Castle Black, 222-222-2222, Ned
Stark, 999 Winterfell Lane, 777-777-7777]]
It correctly printed 3 elements but the elements it printed is the entire array instead of just the appropriate entry. Where am I going wrong? I tried running a for loop and using info.get(i).get(0) + info.get(i).get(1) + info.get(i).get(2), but to no avail. Any ideas will be helpful.