Reputation: 326
Stackoverflow.
I'm trying to add 4 of each words in my ArrayList into the ArrayList itself. I have in my ArrayList two Strings. One is "java" and the other one is "Program". I'm trying to write a program that adds a total of 4 words of each word. Ex: I'm trying to add 4 x java and 4 x program.
Here's what I've got so far. I have no idea, what I'm doing wrong. Any help or hints would be much appreciated.
/*
* Write a method called quadList that takes an ArrayList of strings as a parameter
* and replaces every string with four of that same string.
* For example, if the list stores the values ["java", "program"]
* before the method is called,it should store the values
* ["java ", " java ", " java ", " java ", "program", "program", "program", "program"]
* after the method finishes executing.
*/
import java.util.ArrayList;
public class Ex10_4_quadList {
public static void main(String[] args) {
ArrayList<String> arrList = new ArrayList<String>();
arrList.add("Java");
arrList.add("Program");
System.out.println("Before: " + arrList);
quadList(arrList);
System.out.println("after " + arrList);
}
private static void quadList(ArrayList<String> list) {
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < 4; j++) {
String temp = list.get(i);
list.add(temp);
}
}
}
}
Here's the fixed code:
public class Ex10_4_quadList {
public static void main(String[] args) {
ArrayList<String> arrList = new ArrayList<String>();
arrList.add("Java");
arrList.add("Program");
System.out.println("Before: " + arrList);
quadList(arrList);
Collections.sort(arrList);
System.out.println("after " + arrList);
}
private static void quadList(ArrayList<String> list) {
int initial = list.size();
for (int i = 0; i < initial; i++) {
for (int j = 0; j < 3; j++) {
String temp = list.get(i);
list.add(temp);
}
}
}
}
Upvotes: 0
Views: 22214
Reputation: 27946
Ideally instead of iterating with an index you would use the foreach style of loop. However this means that you can't alter the list as you iterate. So you will need to add the members to a new list and then add all of them afterwards:
List<String> duplicates = new ArrayList<>();
for (String member: list) {
for (int i = 0; i < 4; i++) {
duplicates.add(member);
}
}
list.addAll(duplicates);
There are a number of shortcuts you can use if you are using Java 8 and, therefore, have access to streams:
list.addAll(list.stream()
.flatMap(m -> Stream.generate(() -> m).limit(4))
.collect(Collectors.toList());
This code says for each member of the list turn it into 4 copies of the item then collect all those as a list and add them to the original list.
Upvotes: 3
Reputation: 1896
Try:
private static void quadList(ArrayList<String> list) {
int listSize = list.size();
for (int i = 0; i < listSize; i++) {
for (int j = 0; j < 4; j++) {
String temp = list.get(i);
list.add(temp);
}
}
}
The problem with the code is that the list.size() is evaluated on every iteration. Since the inner loop is increasing the list size faster than the outer loop can iterate over it, the code effectively loops infinitely until JVM runs out of memory.
Upvotes: 1