Array help needed please...

Hey,

I was wondering if anybody could point me in the right direction in regards to creating an array that can store each month of the year, and a number for each.

[january][1]
[february][2] etc...

Thanks in advance...

Comments

  • : Hey,
    :
    : I was wondering if anybody could point me in the right direction in regards to creating an array that can store each month of the year, and a number for each.
    :
    : [january][1]
    : [february][2] etc...
    :
    : Thanks in advance...
    :
    An array element can hold one item, be it a primitive type, string or any other object. Each element also has to be the same type.

    For your situation, create your own month object and store them in each element.

    something like this:

    [code]

    public class Month
    {
    String month;
    int num;

    public Month(String month, int num)
    {
    this.month=month;
    this.num=num;
    }

    //insert the appropriate accessor/mutator methods(ie get and
    set)and perhaps a toString method.

    [/code]

    Now in another class create the array of Month.

    [code]
    Month[] month = new Month[12];

    ...

    //to fill it up, you can use several methods, here is the most basic way
    //to put an object in the array

    month[0] = new Month("january",1);

    [/code]
  • Thanks very much for your help :-) highly appreciated!
  • : Hey,
    :
    : I was wondering if anybody could point me in the right direction in regards to creating an array that can store each month of the year, and a number for each.
    :
    : [january][1]
    : [february][2] etc...
    :
    : Thanks in advance...
    :

  • : : Hey,
    : :
    : : I was wondering if anybody could point me in the right direction in regards to creating an array that can store each month of the year, and a number for each.
    : :
    : : [january][1]
    : : [february][2] etc...
    : :
    : : Thanks in advance...
    : :
    :
    :

    Declare two dimenstional String array
    String month[][];

    and store values as string and when you want the integer value
    use Integer.parseInt() method

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories