By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Need help with 2D array

P: n/a
Would someone shows me the java syntax to create a 2D array of a calendar,
where the rows represents months, such as: Jan, Feb... and columns represent
days; assuming that there are 29 days in Feb? I don't know whether to
declare this 2D array in String or int, since the months are string while
days are int. Thanks
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Khanh Le wrote:
Would someone shows me the java syntax to create a 2D array of a calendar,
where the rows represents months, such as: Jan, Feb... and columns represent
days; assuming that there are 29 days in Feb? I don't know whether to
declare this 2D array in String or int, since the months are string while
days are int. Thanks


Unless I'm misunderstanding, you're simply trying to do something like

int[] number = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

which seems rather pointless - why use number[i] in place of simply i?

Otherwise, the answer is whatever kind of information you want to store
about each date of the year.

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 17 '05 #2

P: n/a
"Khanh Le" <le*******@earthlink.net> wrote in message news:<2f*****************@newsread2.news.pas.earth link.net>...
Would someone shows me the java syntax to create a 2D array of a calendar,
where the rows represents months, such as: Jan, Feb... and columns represent
days; assuming that there are 29 days in Feb? I don't know whether to
declare this 2D array in String or int, since the months are string while
days are int. Thanks


I don't think an array is the best approach. You could read about the
class java.util.Calendar at
http://java.sun.com/j2se/1.4.2/docs/api/
That will let you compute that 29 is the last day of Calendar.FEBRUARY
in 2004, 31 is the last day of Calendar.MARCH in 2003, etc.

To look up an array of int's, starting from a String, you could use a
Hashtable. This is awkward, so I don't recommend it, but I'll show it
to you.

import java.util.Hashtable;

Hashtable h = new Hashtable();
int[] daysJan = new int[31];
for (int i = 0; i < 31; ++i) {
daysJan[i] = i+1;
}
h.put("January", daysJan);
// Do the same for the other months.
// Later...
String nameOfMonth = "January";
int[] daysOfMonth = (int[])h.get(nameOfMonth);
// daysOfMonth is now the int array {1,2,...,31}
Jul 17 '05 #3

P: n/a
Mark McConnell wrote:
"Khanh Le" <le*******@earthlink.net> wrote in message news:<2f*****************@newsread2.news.pas.earth link.net>...
Would someone shows me the java syntax to create a 2D array of a calendar,
where the rows represents months, such as: Jan, Feb... and columns represent
days; assuming that there are 29 days in Feb? I don't know whether to
declare this 2D array in String or int, since the months are string while
days are int. Thanks

I don't think an array is the best approach. You could read about the
class java.util.Calendar at
http://java.sun.com/j2se/1.4.2/docs/api/
That will let you compute that 29 is the last day of Calendar.FEBRUARY
in 2004, 31 is the last day of Calendar.MARCH in 2003, etc.

To look up an array of int's, starting from a String, you could use a
Hashtable. This is awkward, so I don't recommend it, but I'll show it
to you.

import java.util.Hashtable;

Hashtable h = new Hashtable();
int[] daysJan = new int[31];
for (int i = 0; i < 31; ++i) {
daysJan[i] = i+1;
}
h.put("January", daysJan);
// Do the same for the other months.
// Later...
String nameOfMonth = "January";
int[] daysOfMonth = (int[])h.get(nameOfMonth);
// daysOfMonth is now the int array {1,2,...,31}


I implemented a TableModel backed by a Calendar object and it _greatly_
facilitated the viewing of Date in a JTable. All I was left with was
concentrating the data (that is, the method implentation of TableModel)
and the View (JTable) was going to be taken care of.
No layouts, no custom painting, no custom components, etc, no problem;)
-Bryan
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.