Monday 15 April 2013

java - Iterate through list of object array -


I am querying using the following code using the original SQL

  Private & Lt; T & gt; & Lt; T & gt; ExecuteNativeQuery (string query string, map & lt; string, object & gt; supreme, class & lt; T & gt; clues) {query query = entityManager.createNativeQuery (queryString); If (param! = Null & amp; param.size ()> gt; {for (Map.Entry & lt; string, object & gt; entry: param.entrySet ()) {query.setParameter (entry .getKey), entry .getValue ()); }} List & lt; T & gt; Result list = query.getResultList (); Return results list; }   

and getting database results.

  Seller name | IncidentID | IncidentStatus | Event day ------------------------------------------------ - ------ XYZ | 100 | Open | 02-Jun-2011 ABC 101 | Off | 03-Jun-2011 MNP | 102 | Open | 01-Jun-2011 LPQ | 103 | Open | 01-APR-2011   

To iterate this result, I am using the following approach

  Iterator iter = resultList.iterator () ; While (iter.hasNext ()) {object [] result = (object []) iter.next (); System.out.println ("vendor name -> gt; + result [0]); }   

Is there a better way to iterate in the list?

A "better way" (java 5+ for each) actually showed what you would translate , Using an iterator, but the syntax is good:

  for (object [] result: resultList) {// ...   < P> Another "better way" depends on the fact that you know the list size and you can use a counter in the loop:  
 for  (int i = 0; I & lt; resultList.size (); i ++) {object [] Inam = Prinamlistkget (i); // ... the rest ...   

In short, there are several ways to recycle through the list, but it is logical that there are "better" ways: this is the case- Case (if you are using that i counter for other things or you're using iterator to remove some elements from the list) or simply A taste case (low-level vs syntax-sugar)

No comments:

Post a Comment