Reputation: 45
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);
}
Upvotes: 1
Views: 13241
Reputation: 12754
You can do it like this:
short[] years = {1995, 1997, 1998,1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012};
String[] winners = {"January", "February", "June", "January", "March", "June", "July", "August", "September", "March", "May", "January", "March", "July", "November", "March", "June"};
System.out.println("Years"+"\t"+"Premiers");
int i = years.length-1;
int j= winners.length-1;
int k=0;
int l=0;
do{
i--;
j--
System.out.println(years[k++]+"\t"+winners[l++]);
}while(i>=0 || j>=0);
You will found the output like:
Years Premiers
1995 January
1997 February
1998 June
1999 January
2000 March
2001 June
2002 July
2003 August
2004 September
2005 March
2006 May
2007 January
2008 March
2009 July
2010 November
2011 March
2012 June
Upvotes: 1
Reputation: 2644
try this:
int length = Years.length > Months.length ? Months.length : Years.length;
for(int index = 0; index < length; index++) {
System.out.println(Years[index] + '\t' + Months[index]);
}
And i suggest reading this
Upvotes: 2
Reputation: 891
If they are of equal length:
for (int i = 0; i < Years.length; i++) {
System.out.println(Years[i] + '\t' + Months[i]);
}
Upvotes: 2