Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an String ArrayList in Java with a repeating pattern

Tags:

java

arraylist

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"
like image 562
jpm Avatar asked Feb 22 '26 23:02

jpm


2 Answers

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());
like image 130
Mureinik Avatar answered Feb 24 '26 12:02

Mureinik


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");
}
like image 25
Mustafa sabir Avatar answered Feb 24 '26 12:02

Mustafa sabir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!