Skip to main content
deleted 21 characters in body; edited tags
Source Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157

Iterator Patternpattern/Iterator Classiterator class

I have implemented a simple iterator. Just want to check if I have missed anything.

interface iterator<E>
    {
        boolean hasNext();
    
        E next();
    }
    
    class Iterator<E> implements iterator
    {
        E[] arr;
    
        int curPos = 0;
    
        @Override
        public boolean hasNext()
        {
            if (curPos >= arr.length || arr[curPos] == null) 
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    
        @Override
        public E next()
        {
            if(hasNext())
            {
                E res = arr[curPos];
                curPos++;
                return arr[curPos+1];
            }
            else
            {
                throw new runtimeException("No next element available !");
            }
        }
    
    }

questionQuestions:

  1. Why should I not use ObjectObject instead of EE/template everywhere ?
  2. How do I make an iterator for a binary tree.

I assume apart from the above, I would have to implement a traversal algorithm and try to find if the next "traversal" successor exist?

Thanks already!

Iterator Pattern/Iterator Class

I have implemented a simple iterator. Just want to check if I have missed anything.

interface iterator<E>
    {
        boolean hasNext();
    
        E next();
    }
    
    class Iterator<E> implements iterator
    {
        E[] arr;
    
        int curPos = 0;
    
        @Override
        public boolean hasNext()
        {
            if (curPos >= arr.length || arr[curPos] == null) 
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    
        @Override
        public E next()
        {
            if(hasNext())
            {
                E res = arr[curPos];
                curPos++;
                return arr[curPos+1];
            }
            else
            {
                throw new runtimeException("No next element available !");
            }
        }
    
    }

question:

  1. Why should I not use Object instead of E/template everywhere ?
  2. How do I make an iterator for a binary tree.

I assume apart from the above, I would have to implement a traversal algorithm and try to find if the next "traversal" successor exist?

Thanks already!

Iterator pattern/iterator class

I have implemented a simple iterator. Just want to check if I have missed anything.

interface iterator<E>
    {
        boolean hasNext();
    
        E next();
    }
    
    class Iterator<E> implements iterator
    {
        E[] arr;
    
        int curPos = 0;
    
        @Override
        public boolean hasNext()
        {
            if (curPos >= arr.length || arr[curPos] == null) 
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    
        @Override
        public E next()
        {
            if(hasNext())
            {
                E res = arr[curPos];
                curPos++;
                return arr[curPos+1];
            }
            else
            {
                throw new runtimeException("No next element available !");
            }
        }
    
    }

Questions:

  1. Why should I not use Object instead of E/template everywhere ?
  2. How do I make an iterator for a binary tree.

I assume apart from the above, I would have to implement a traversal algorithm and try to find if the next "traversal" successor exist?

Improved formatting on questions to make them more readable.
Source Link

I have implemented a simple iterator. Just want to check if I have missed anything.

interface iterator<E>
    {
        boolean hasNext();
    
        E next();
    }
    
    class Iterator<E> implements iterator
    {
        E[] arr;
    
        int curPos = 0;
    
        @Override
        public boolean hasNext()
        {
            if (curPos >= arr.length || arr[curPos] == null) 
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    
        @Override
        public E next()
        {
            if(hasNext())
            {
                E res = arr[curPos];
                curPos++;
                return arr[curPos+1];
            }
            else
            {
                throw new runtimeException("No next element available !");
            }
        }
    
    }

question: 1]Why should I not use Object instead of E/template everywhere ?
2] How do

  1. Why should I not use Object instead of E/template everywhere ?
  2. How do I make an iterator for a binary tree.

I make an iterator for a binary tree. I assume apart from the above  , I would have to implement a traversal algorithm and try to find if the next "traversal" successor exist  ?
Thanks

Thanks already!

I have implemented a simple iterator. Just want to check if I have missed anything.

interface iterator<E>
    {
        boolean hasNext();
    
        E next();
    }
    
    class Iterator<E> implements iterator
    {
        E[] arr;
    
        int curPos = 0;
    
        @Override
        public boolean hasNext()
        {
            if (curPos >= arr.length || arr[curPos] == null) 
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    
        @Override
        public E next()
        {
            if(hasNext())
            {
                E res = arr[curPos];
                curPos++;
                return arr[curPos+1];
            }
            else
            {
                throw new runtimeException("No next element available !");
            }
        }
    
    }

question: 1]Why should I not use Object instead of E/template everywhere ?
2] How do I make an iterator for a binary tree. I assume apart from the above  , I would have to implement a traversal algorithm and try to find if the next "traversal" successor exist  ?
Thanks already!

I have implemented a simple iterator. Just want to check if I have missed anything.

interface iterator<E>
    {
        boolean hasNext();
    
        E next();
    }
    
    class Iterator<E> implements iterator
    {
        E[] arr;
    
        int curPos = 0;
    
        @Override
        public boolean hasNext()
        {
            if (curPos >= arr.length || arr[curPos] == null) 
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    
        @Override
        public E next()
        {
            if(hasNext())
            {
                E res = arr[curPos];
                curPos++;
                return arr[curPos+1];
            }
            else
            {
                throw new runtimeException("No next element available !");
            }
        }
    
    }

question:

  1. Why should I not use Object instead of E/template everywhere ?
  2. How do I make an iterator for a binary tree.

I assume apart from the above, I would have to implement a traversal algorithm and try to find if the next "traversal" successor exist?

Thanks already!

added 46 characters in body
Source Link
p101
  • 343
  • 1
  • 4
  • 9

I have implemented a simple iterator. Just want to check if I have missed anything.

interface iterator<E>
    {
        boolean hasNext();
    
        E next();
    }
    
    class Iterator<E> implements iterator
    {
        E[] arr;
    
        int curPos = 0;
    
        @Override
        public boolean hasNext()
        {
            if (curPos >= arr.length || arr[curPos] == null) 
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    
        @Override
        public E next()
        {
            if(hasNext())
            {
                E res = arr[curPos];
                curPos++;
                return arr[curPos+1];
            }
            else
            {
                returnthrow null;new runtimeException("No next element available !");
            }
        }
    
    }

question: 1]Why should I not use Object instead of E/template everywhere ?
2] How do I make an iterator for a binary tree. I assume apart from the above , I would have to implement a traversal algorithm and try to find if the next "traversal" successor exist ?
Thanks already!

I have implemented a simple iterator. Just want to check if I have missed anything.

interface iterator<E>
    {
        boolean hasNext();
    
        E next();
    }
    
    class Iterator<E> implements iterator
    {
        E[] arr;
    
        int curPos = 0;
    
        @Override
        public boolean hasNext()
        {
            if (curPos >= arr.length || arr[curPos] == null) 
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    
        @Override
        public E next()
        {
            if(hasNext())
            {
                E res = arr[curPos];
                curPos++;
                return arr[curPos+1];
            }
            else
            {
                return null;
            }
        }
    
    }

question: 1]Why should I not use Object instead of E/template everywhere ?
2] How do I make an iterator for a binary tree. I assume apart from the above , I would have to implement a traversal algorithm and try to find if the next "traversal" successor exist ?
Thanks already!

I have implemented a simple iterator. Just want to check if I have missed anything.

interface iterator<E>
    {
        boolean hasNext();
    
        E next();
    }
    
    class Iterator<E> implements iterator
    {
        E[] arr;
    
        int curPos = 0;
    
        @Override
        public boolean hasNext()
        {
            if (curPos >= arr.length || arr[curPos] == null) 
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    
        @Override
        public E next()
        {
            if(hasNext())
            {
                E res = arr[curPos];
                curPos++;
                return arr[curPos+1];
            }
            else
            {
                throw new runtimeException("No next element available !");
            }
        }
    
    }

question: 1]Why should I not use Object instead of E/template everywhere ?
2] How do I make an iterator for a binary tree. I assume apart from the above , I would have to implement a traversal algorithm and try to find if the next "traversal" successor exist ?
Thanks already!

added 172 characters in body
Source Link
p101
  • 343
  • 1
  • 4
  • 9
Loading
added 178 characters in body; added 2 characters in body; added 110 characters in body
Source Link
p101
  • 343
  • 1
  • 4
  • 9
Loading
Tweeted twitter.com/#!/StackCodeReview/status/77118512950288384
Source Link
p101
  • 343
  • 1
  • 4
  • 9
Loading