I would like to take each element of ArrayList and use it to create a String that:
- contains as many words as elements of the ArrayList and,
- the int value from the ArrayList is printed for each of the words.
To make it clearer I want to print a String that will look like this:
System.out.println(result);
element(0), element(1), element(2), element(3)
Unfortunately, I'm only getting the value of the last Integer from the ArrayList, but the number of 'element' words is correct, so my actual result String looks like that:
System.out.println(result);
element(3), element(3), element(3), element(3)
The ArrayList has only 4 elements:
[0, 1, 2, 3]
To produce this incorrect String I'm using the following code:
List<Integer> intList = new ArrayList<Integer>();
int intValue;
String result;
int n = intList.size();
for (int i=0; i < n; i++) {
intValue = intList.get(i);
result = String.join(", ", java.util.Collections.nCopies(n, "element("+intValue+")"));
}
So how can I make a correct String with all values of the ArrayList?