Use a second ArrayList for the 3 strings, not a primitive array. Ie.
private List<List<String>> addresses = new ArrayList<List<String>>();
Then you can have:
ArrayList<String> singleAddress = new ArrayList<String>();
singleAddress.add("17 Fake Street");
singleAddress.add("Phoney town");
singleAddress.add("Makebelieveland");
addresses.add(singleAddress);
(I think some strange things can happen with type erasure here, but I don't think it should matter here)
If you're dead set on using a primitive array, only a minor change is required to get your example to work. As explained in other answers, the size of the array can not be included in the declaration. So changing:
private ArrayList<String[]> addresses = new ArrayList<String[3]>();
to
private ArrayList<String[]> addresses = new ArrayList<String[]>();
will work.
Answer from Grundlefleck on Stack OverflowUse a second ArrayList for the 3 strings, not a primitive array. Ie.
private List<List<String>> addresses = new ArrayList<List<String>>();
Then you can have:
ArrayList<String> singleAddress = new ArrayList<String>();
singleAddress.add("17 Fake Street");
singleAddress.add("Phoney town");
singleAddress.add("Makebelieveland");
addresses.add(singleAddress);
(I think some strange things can happen with type erasure here, but I don't think it should matter here)
If you're dead set on using a primitive array, only a minor change is required to get your example to work. As explained in other answers, the size of the array can not be included in the declaration. So changing:
private ArrayList<String[]> addresses = new ArrayList<String[3]>();
to
private ArrayList<String[]> addresses = new ArrayList<String[]>();
will work.
List<String[]> addresses = new ArrayList<String[]>();
String[] addressesArr = new String[3];
addressesArr[0] = "zero";
addressesArr[1] = "one";
addressesArr[2] = "two";
addresses.add(addressesArr);
Videos
In Java 8 or later:
String listString = String.join(", ", list);
In case the list is not of type String, a joining collector can be used:
String listString = list.stream().map(Object::toString)
.collect(Collectors.joining(", "));
If you happen to be doing this on Android, there is a nice utility for this called TextUtils which has a .join(String delimiter, Iterable) method.
List<String> list = new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
String joined = TextUtils.join(", ", list);
Obviously not much use outside of Android, but figured I'd add it to this thread...
Hi, I'm doing homework for my computer science class and part of the homework is making an ArrayList that returns 3 inputed string values in a specific order.
When I run the program I get an output of: [Ljava.lang.String;@3cd1f1c8"}instead of ["specialist", "inventory", "add"]
Anyone know what I'm doing wrong? Here's the section of code:
https://imgur.com/a/nvqj1