473,395 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Need help with 2D array

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
3 7613
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Nathan Rose | last post by:
Here's my problem. I am reading from a text file using this: if (!file_exists($file)) { echo "Don't exist\n"; return false; } $fd = @fopen($file, 'r'); if (!is_resource($fd))
4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
48
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
2
by: Thomas Connolly | last post by:
Anyone know if there is a C# equivallent to: enum { LIFFE_SIZE_AUTOMARKETREF = 15 }; typedef char LiffeAutoMarketReference ; Thanks,
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
5
by: SpreadTooThin | last post by:
If you are deriving a new class from another class, that you must (I assume) know the initializer of the other class. So in myClass import array class myClass(arrary.array): def...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
1
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.