• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

iterator questions ...

Hello experts,

   Can anyone please tell me what I did wrong from line 00 - lin02    


    List lineArray = new ArrayList();
    Iterator it = lineArray.iterator();

     if (it.next().toString().equals("DataDictionary")) {
                if (pmml.getDataDictionary() == null) {
                    DataDictionary dataDictionary = pmml.addNewDataDictionary();
                    String str1 = it.toString();   // line 00
                    myPath.add(str1);    // line01
                   
                    System.out.println("path " + str1);  // line02
                }
              :
              :
0
meow00
Asked:
meow00
2 Solutions
 
NeoEGMCommented:
Hi meow00!

Shouldn't the code be this way:

----------------------- CODE ---------------------------
List lineArray = new ArrayList();
Iterator it = lineArray.iterator();

String currStr = "";

while (!currStr.equals("DataDictionary") && it.hasNext())     //You iterate the array to find the correct object
{
     currStr = it.next().toString();
}

if (currStr.equals("DataDictionary"))     //If there was a found item
{
     if (pmml.getDataDictionary() == null)
     {
          DataDictionary dataDictionary = pmml.addNewDataDictionary();

          myPath.add(currStr);    //When you called "next()" again, you lost the previous object
                   
          System.out.println("path " + currStr);
     }
}
------------------------------------------------------------

?

Regards,
NeoEGM
0
 
amol_chaudhariCommented:
hi
in ur code u just created arraylist and gets its iterator. there are no objects in the list
but still u are calling itr.next() this may cause an exception

so it is better to check if next element is available or not

ur code should be as

List lineArray = new ArrayList();
Iterator it = lineArray.iterator();

if(it.hasNext()) {
     if( ((String)it.next()).equals("DataDictionary") ) {
        // ..................
     }
}
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now