Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

iterate

 
Ritika Misra
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers,

My code



My output :




Can any one help me ..with this output...how can i convert this out put to an arrayList......
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i convert this out put to an arrayList


By looking at your System.out statement and the output,
Just declare an ArrayList of type of the objects you are displaying. Add those objects to the list as you iterate.



Is this what you want?
 
prasad guna
Ranch Hand
Posts: 46
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just declare the arraylist object
but arraylist allow duplicates..



hope will use..
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prasad guna wrote:Hi,

Just declare the arraylist object
but arraylist allow duplicates..



hope will use..

Just make sure to declare and initialize the array list outside of the loop. Otherwise you will have many array lists, each with only one element, that is ready for garbage collection immediately.
 
Ritika Misra
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well guys,
i dont want to add it as it is appearing in the output.
I want to add to my data to a POJO first and then that POJO to ArrayList .
Somewat like this ....

POJO Name =Transaction
skeleton


i want to set the value of column id to id ,name to name and so on...
then these each Transcation object to arrayList.

so how do i do this

thanks in advance
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you have to populate POJOs with the values retrieved from database while iterating the loop and add each of them to the list unless you are using a O/R Mapping framework like Hibernate which will load POJOs for you from the database.
 
Ritika Misra
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know that ..
but as you can see my out put .....
this data which i get...is coming from Access database
so i m not getting in the regular format through which i can populate my POJO..


 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





What ever you are getting you assigned to a type Object here. What type is that object? Isn't that the type you want (i.e: type of your POJO) ?
 
Ritika Misra
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Vijitha Kumara,

As you suggest ,
What ever you are getting you assigned to a type Object here. What type is that object? Isn't that the type you want


if i do this,




And this error is valid as it is a linked hashMap ....so please suggest some way out for retreiving data from LinkedHashMap
I get an error ....pasting error below,
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that is a Map then you retrieve the values using the keys (as you know the keys are id,name,etc..) and set them in the POJO.



And make sure the types of properties of the POJO match with what you retrieve.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic