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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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") ) {
        // ..................
     }
}
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.