Search code examples
javaparallel-arrays

Side by Side display of parallel arrays in java


I'm quite new to java and I've written the code to display the values of the following parallel arrays:

short[] Years = {1995, 1997, 1998,1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012};

String[] Months = {"January", "February", "June", "January", "March", "June", "July", "August", "September", "March", "May", "January", "March", "July", "November", "March", "June"};

Currently when I run it, they display on top each other. I'm trying to get it to display side by side. How can I do that?

This is the piece of code for displaying them:

System.out.println("Years");
for(short temp: years)
{
System.out.println(temp);
}
System.out.println("Months");
for(String temp: months)
{
System.out.println(temp);
}

Solution

  • If they are of equal length:

    for (int i = 0; i < Years.length; i++) {
        System.out.println(Years[i] + '\t' + Months[i]);
    }