Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advice Designing Table Oriented Solution

Status
Not open for further replies.

bper123

Programmer
Jan 24, 2006
10
US
Hi,

I need advice on the best way to handle a table or matrix solution. For example, if I have a table such as:

A B C D
1 true false false true

2 false false false false

3 false true false false

4 false false false true

I don't want to code a bunch of 'if' statements to return the value contained in the table based on the coordinates. Doing so would be messy and hard to extend and maintain.

I know that there must be a better way to solve this in an OOP approach. I need to do this in Java. Any suggestions would be appreciated.
 
Not sure I understand the problem. Why do you need 'if' statements to get a value out of a two-dimensional array?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Sorry for the confusion. The table, or array, isn't as straight forward as it appears. Each row and column is a test condition.

For example:

What if each row represented a day of the week and each column represented some other condition and both conditions were parameters for which your processing needs to return a value.

Such as:

If the day is Friday and it's raining,then return eat soup. There can be several possibilities for each day, but the return values are constant for their respective combinations. Also imagine that there are more than 7 days of the week, and it's even possible to extend more days in the future and more conditions (rows & columns).
 
It sounds as though it IS as straightforward as it seems. Consider a two-dimensional array of strings, 7 x 2 (MTWTFSS) x (T/F). Monday + raining => table(0, 0) = "eat soup".

If you have 'names' for your conditions, I guess you could use a couple of hashmaps to convert the string names into row and column numbers?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top