Mohamad Dirieh Mohamad Dirieh - 9 days ago 8
Java Question

Looping by 2 index of char

Please I need help to loop into an ArrayList :


[PR, PS, QS, QT, RP, RT, SP, SQ, TQ, TR]


So that my output be like this :

1) PR PS
2) QS QT
3) RP RT
4) SP SQ
5) TQ TR


Notice that once I read the first 2 , i am jumping to the third an fourth index;
Thank you beloved community of Stack

Answer

try this simple code

    String [] arr = {"PR", "PS", "QS", "QT", "RP", "RT", "SP", "SQ", "TQ", "TR"};
    List<String> list = Arrays.asList(arr);

    for (int i = 0; i < list.size() - 1; i = i + 2) {
        System.out.printf("%s %s %n", list.get(i), list.get(i + 1));
    }

or if you want the numbering

System.out.printf("%d) %s %s %n", (i / 2) + 1, list.get(i), list.get(i + 1));