Arrays of Arrays in Java,
This is a nasty one for me… I’m a PHP guy working in Java on a JSP project. I know how to do what I’m attempting through too much code and a complete lack of finesse.
I’d prefer to do it right. Here is the situation:
I’m writing a small display to show customers what days they can water their lawns based on their watering group (ABCDE) and what time of year it is. Our seasons look like this:
Summer (5-1 to 8-31)
Spring (3-1 to 4-30)
Fall (9-1 to 10-31)
Winter (11-1 to 2-28)
An example might be:
If I’m in group A, here would be my allowed times:
Winter: Mondays only
Spring: Tues, Thurs, Sat
Summer: Any Day
Fall: Tues, Thurs, Sat
If I was writing this in PHP I would use arrays like this:
//M=Monday,t=Tuesday,T=Thursday.... etc $schedule["A"]["Winter"]='M'; $schedule["A"]["Spring"]='tTS'; $schedule["A"]["Summer"]='Any'; $schedule["A"]["Fall"]='tTS'; $schedule["B"]["Winter"]='t';
I COULD make the days arrays (array(“Tuesday”,”Thursday”,”Saturday”)) etc, but it is not necessary for what I’m really trying to accomplish.
I will also need to setup arrays to determine what season I’m in:
Can anyone suggest a really cool way to do this? I will have today’s date and the group letter. I will need to get out of my function a day (M) or a series of days (tTS), (Any).
You could do essentially the same code with Hashtables (or some other Map):
> schedule = new Hashtable >(); schedule.put("A", new Hashtable ()); schedule.put("B", new Hashtable ()); schedule.put("C", new Hashtable ()); schedule.put("D", new Hashtable ()); schedule.put("E", new Hashtable ()); schedule.get("A").put("Winter", "M"); schedule.get("A").put("Spring", "tTS"); // Etc...
Not as elegant, but then again, Java isn’t a dynamic language, and it doesn’t have hashes on the language level.
Note: You might be able to do a better solution, this just popped in my head as I read your question.
That’s the answer Arrays of Arrays in Java, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.
The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you