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
                }
              :
              :
LVL 1
meow00Asked:
Who is Participating?
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.