java - Is it okay to have duplicates in a array? -


i'm working on self project. project let input day , program gives day of given date.

here's array:

string []days = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday", "sunday", "monday"}; 

my condition:

if(inputdate <= 6){                   firstcase = yearscode[0] + inputdate + 3 - 7;                   system.out.println("january " + inputdate + " " + days[firstcase]);} 

i put day sunday , monday again because if input date 5<7 gives me arrayoutofboundsexception.

i got algorithm here #6:

arrays don't care put in them, can have several copies of object in array if desire.

however, solve problem more elegantly, can ensure index calculate never exceeds number of elements in array. this, can use modulo operator %

    firstcase = yearscode[0] + inputdate + 3 - 7;    firstcase = firstcase % 7; 

Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -