2

How can I create a String ArrayList with a repeated group of strings like

"A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", ......

In Python I use

list = deque(["A","B","C"]*288) # 288 times "A","B","C"
1
  • 1
    you could create an empty ArrayList and add the Strings with a for loop Commented Jan 14, 2017 at 11:55

3 Answers 3

6

You could use an IntStream to create a range of 288 items, and then flatmap it to though three strings:

List<String> strings = IntStream.range(0, 288)
                                .boxed()
                                .flatMap(i -> Stream.of("A", "B", "C"))
                                .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

3

Without complicating things, Use a simple for loop:

ArrayList<String> alphabets = new ArrayList<String>();

for (int i=0; i<288; i++) {
    alphabets.add("A");
    alphabets.add("B");
    alphabets.add("C");
}

Comments

1

Try this

for (int i = 0; i < 288; i++) {
    arrayListName.add((3 * i), "A");
    arrayListName.add((3 * i + 1), "B");
    arrayListName.add((3 * i + 2), "C");
}

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.