0

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);

        }

    }

}

 }
1
  • 1
    In your for loop, you are checking this i < list.size(). Since you are adding items to the list in the loop, it will change across the iterations. What you might want to consider doing is int initial = list.size(); before the loop and change the checking condition to 'i < initial'. However if you want those to be sequential, you would need to use a different add add(int index, E element) Commented Feb 5, 2015 at 21:52

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm still running Java 7 - but I'll update to 8 soon enough and start looking into "streams" and whatelse is new :P Thanks for the help though.I got it fixed :P
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.