0

So I want the result to be "Today Blue" and "Tomorrow Red" how can I do that by looping?

String[] sections = 
        {
          "Blue",
          "Red"
        };

       String[] stringDay = 
        {
          "Today",
          "Tomorrow"
        };

    for(String sectionsLoop : sections)
    {
        System.out.println(stringDay + " " + sectionsLoop);
    }

2 Answers2

1
public static void main(String... args) {

  String[] selection =  new String[] {"Blue","red"};
  String[] day = new String[] {"today","tomorrow"};

  for(int i = 0 ; i < s.length ; ++i) {
      System.out.println(day[i] + " " + selection[i]);
  }
}
Abhinav Chauhan
  • 1,304
  • 1
  • 7
  • 24
0

You can use the traditional for loop but you have to make sure both arrays from the same size or do the below to prevent arrayOutOfIndex

    int size = sections.length > stringNumber.length ?sections.length : stringNumber.length;
    for (int i = 0; i <size ; i++) {
        if (sections.length < i) {
            System.out.println(sections[i]);
        }
        if (stringNumber.length < i) {
            System.out.println(stringNumber[i]);
        }
    }
AMA
  • 408
  • 3
  • 9