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 - ScrollTo Effect (href to div) -

javascript - ng-class not responding to change in model in Angular? -

javascript - document.registerElement extending HTMLInputElement prototype while using custom tag name to avoid implicit browser style -