Assign Strings to a Date List

2

I need to allocate for each day a list of dates of a range period, a certain String out of three possible (Class A, Class B, Class C), so that the result is sequential until all the dates in the list are filled out .

Follow the code made so far ....

import java.util.*;
import java.text.*;

public class CalcularDatas {

    public static void main (String[] args) throws ParseException {

        String[] myArray = {"Turma A", "Turma B", "Turma C"};
        List<String> arrayList = new ArrayList<String> (Arrays.asList(myArray));

        DateFormat df = new SimpleDateFormat ("dd/MM/yyyy");
        Date dt1 = df.parse ("01/10/2016"); // Data inicial
        Date dt2 = df.parse ("10/10/2016"); // 
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime (dt1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime (dt2);
        cal2.add(Calendar.DATE,1);
        for (Calendar cal = cal1; cal.compareTo (cal2) <= 0; cal.add (Calendar.DATE, 1)) {
            System.out.println (df.format (cal.getTime())+ arrayList);
        }
    }
}

Output:

  

01/10/2016 [Class A, Class B, Class C]
  02/10/2016 [Class A, Class B, Class C]
  03/10/2016 [Class A, Class B, Class C]
  04/10/2016 [Class A, Class B, Class C]
  05/10/2016 [Class A, Class B, Class C]
  06/10/2016 [Class A, Class B, Class C]
  07/10/2016 [Class A, Class B, Class C]
  10/08/2016 [Class A, Class B, Class C]
  09/10/2016 [Class A, Class B, Class C]
  10/10/2016 [Class A, Class B, Class C]
  11/10/2016 [Class A, Class B, Class C]

Expected Mode:

  

01/10/2016 Class A   10/02/2016 Class B   03/10/2016 Class C   10/4/2016 Class A   05/10/2016 Class B   10/06/2016 Class C   10/7/2016 Class A   10/10/2016 Class B   10/9/2016 Class C   10/10/2016 Class A   10/11/2016 Class B

    
asked by anonymous 12.11.2016 / 20:40

2 answers

3

If you print arrayList , obviously it will print it whole. The solution is to use an iterator, increase it modulus, and print only the position of that index:

int i = 0;
for (Calendar cal = cal1; cal.compareTo(cal2) <= 0; cal.add(Calendar.DATE, 1)) {
    System.out.println(df.format(cal.getTime()) + " " + arrayList.get(i));
    i = (i + 1) % 3;
}
    
12.11.2016 / 21:14
2

One of the possible workarounds is to use Iterator under your ArrayList :

public static void main (String[] args) {
    String[] myArray = {"Turma A", "Turma B", "Turma C"};
    List<String> arrayList = new ArrayList<String> (Arrays.asList(myArray));

    DateFormat df = new SimpleDateFormat ("dd/MM/yyyy");
    Date dt1 = df.parse ("01/10/2016"); // Data inicial
    Date dt2 = df.parse ("10/10/2016"); // 
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime (dt1);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime (dt2);
    cal2.add(Calendar.DATE,1);
    //cria o iterator sob o arraylist
    Iterator<String> it = arrayList.iterator();

    for (Calendar cal = cal1; cal.compareTo (cal2) <= 0; cal.add (Calendar.DATE, 1)) {
        //checa se o iterator já chegou ao fimm
        //se sim, o reinicia
        if(!it.hasNext()){
            it = arrayList.iterator();      
        }
        // next() exibe o item seguinte do arraylist
        System.out.println (df.format (cal.getTime()) + it.next());
    }
}

Result:

  

01/10/2016 Class A   10/02/2016 Class B   03/10/2016 Class C   10/4/2016 Class A   05/10/2016 Class B   10/06/2016 Class C   10/7/2016 Class A   10/10/2016 Class B   10/9/2016 Class C   10/10/2016 Class A   10/11/2016 Class B

See working at IDEONE

    
12.11.2016 / 21:14