Two dimensional array problem

J

Juggernaut

I have a two dimensional array, [12][31], and I have it made as a
calender where the days are filled with some information. But some
months, like february only has 28 days so I havent filled 29 30 and
31. But now I want to make some calculations on these numbers, and
divide them by all the days. But the problem is they also count the
array fields like february 29 , 30 and 31 wich is empty..I tried a for
loop where if col < table[row].length && table[row][col] != null. But
that didnt work, how can I do this?

Someone suggested for me to try
yourArray[1] = new int SomeType[28];

But java didnt accept this.

Any advice would be gratefull.
 
M

Marco Schmidt

Juggernaut:
Someone suggested for me to try
yourArray[1] = new int SomeType[28];

But java didnt accept this.

Try

yourArray[1] = new int[28];

If yourArray is of type int[][], that should work.

I don't know what exactly you are trying to do, but it seems like you
could profit from encapsulating the array and provide methods like
getDaysPerMonth(int month). That may make the code more readable and
maintainable.

Regards,
Marco
 
J

Juggernaut

Thats what I first did, I had a switch.
But what it did was fill for example february with 28 values, and left the 3
left cells clear. But I dont want those remaining cells to be clear, I want
them to not exist. Thereby having a ragged array.
Marco Schmidt said:
Juggernaut:
Someone suggested for me to try
yourArray[1] = new int SomeType[28];

But java didnt accept this.

Try

yourArray[1] = new int[28];

If yourArray is of type int[][], that should work.

I don't know what exactly you are trying to do, but it seems like you
could profit from encapsulating the array and provide methods like
getDaysPerMonth(int month). That may make the code more readable and
maintainable.

Regards,
Marco
 
J

Jon A. Cruz

Juggernaut said:
But I dont want those remaining cells to be clear, I want
them to not exist. Thereby having a ragged array.

Another common term is "jagged"
 
A

Andrew Hobbs

Juggernaut said:
Thats what I first did, I had a switch.
But what it did was fill for example february with 28 values, and left the 3
left cells clear. But I dont want those remaining cells to be clear, I want
them to not exist. Thereby having a ragged array.

To specifically answer your question how about

int[][] year = new int[][] { new int[31], new int[28], new int[31],
new int[30], new int[31], new int[30], new int[31],
new int[31], new int[30], new int[31], new int[30],
new int[31], };

That will give you a jagged array.

It will also give you difficult to read code. Eminently forgettable code and
probably rather fragile code. It also doesn
t make provision for leap years.

As Marco said why not encapsulate your code.

Why not write a day class which accepts whatever values you need and does
the calculations you want to do. Then write a month class which holds day
objects. These can validate the number of days, make provision for 29 vs 28
days in February depending upon year etc. Then a year class holds month
objects, providing names to months etc. By providing names to objects you
will find it much easier to see what is going on and to rectify problems.

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
Australia

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************
Marco Schmidt said:
Juggernaut:
Someone suggested for me to try
yourArray[1] = new int SomeType[28];

But java didnt accept this.

Try

yourArray[1] = new int[28];

If yourArray is of type int[][], that should work.

I don't know what exactly you are trying to do, but it seems like you
could profit from encapsulating the array and provide methods like
getDaysPerMonth(int month). That may make the code more readable and
maintainable.

Regards,
Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top