• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieve a column value from ArrayList  RSS feed

 
yudi raj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to retrieve a particular column name from Array List. I have the table in SQL as below:

+--------+----------+--------+---------------------------+
| day_id | day_name | temperature_in_fahrenheit |
+--------+----------+--------+---------------------------+
| 1 | Mon | 25 |
| 2 | Tue | 35 |
| 3 | Wed | 45 |
| 4 | Thu | 55 |
| 5 | Fri | 65 |
+--------+----------+--------+---------------------------+




I have made a jdbc connection and added the values to the ArrayList. I have to retrieve only temperature values from above so that I could calculate the temperature values in centigrades and store them in a session. However, I dont know how to approach. Please let me know if you can help me. Thank you.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uday -
1. Why not just have two Lists - one for day (Mon, Tue...) and one for temperature?
2. If days seems to be unique (I mean you take only one week at a time) then you can have it as a Map with day as key and temperature as values.

Still if you need to iterate the same List you can take the odd values(1,3,5) from the current List you have and skip the even values - that will give the temperature.

And I think iterating only the odd values will give you the temperature, as List preserves the order of insertion.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:Uday -
1. Why not just have two Lists - one for day (Mon, Tue...) and one for temperature?


Because that kind of "parallel list" model subverts the point of OO programming and just makes things harder to deal with overall.

There should be one list, whose elements are objects that have a day and a temperature.

 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
John Jai wrote:Uday -
1. Why not just have two Lists - one for day (Mon, Tue...) and one for temperature?


Because that kind of "parallel list" model subverts the point of OO programming and just makes things harder to deal with overall.

There should be one list, whose elements are objects that have a day and a temperature.


+1

Although, if it is guaranteed that each day will occur only once, a Map could also be a viable option. (Day -> Temperature)
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff and Riaan - Thank you!
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree. You can create a DailyTemperature class. I presume this app is for some sort of meteorological use.
 
yudi raj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your time guys. Your solutions were definitely useful. Thanks again.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!