How to implement the Iterable<> method in a generic class

GorillaSpoon

I'm creating a generic class SortedArray that implements Iterable. My big problem is that I'm lost as to what I need to return for this particular method, or for that matter if I'm missing a major step in implementing it. With this current code when I compile it in Unix I'm getting an error stating inconvertible types. Any feedback or information that could put me on my way to fixing this would be much appreciated. Thank you.

public class SortedArray< T extends Comparable< T > >
    extends Object
    implements Iterable < T > {

    private T[] internalArray;
    public Iterator<T> iterator() {
        return (Iterator<T>) Arrays.asList(internalArray).iterator();
    }
}
Luiggi Mendoza

Usually, you create an inner class that implements Iterator interface and that's what you return in iterator method. Here's an example:

//removed extends Object. The compiler will add this info for you
public class SortedArray<T extends Comparable<T>> implements Iterable<T> {

    private T[] internalArray;

    private class SortedArrayIterator implements Iterator<T> {
        int currentIndex;
        public SortedArrayIterator() {
            this.currentIndex = 0;
        }
        @Override
        public boolean hasNext() {
            return currentIndex < maxSize;
        }
        @Override
        public T next() {
            return internalArray[currentIndex++];
        }
        @Override
        public void remove() {
            for (int i = currentIndex; i < internalArray.length - 1; i++) {
                internalArray[i] = internalArray[i+1];
            }
        }
    }

    public Iterator<T> iterator() {
        return new SortedArrayIterator();
    }
}

Not directly related to your question, but there are few problems when working with generic arrays. Make sure you create this private T[] internalArray using the right approach, which is storing Class<T> somewhere, or maintain a Comparable[] internalArray instead, as shown in How to create a generic array in Java?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to just get the method in the implement class with a generic interface in Java

From Dev

How to implement custom iterable class in VBA

From Dev

How implement an interface Generic Method

From Dev

How to implement a generic method in java

From Dev

How implement an interface Generic Method

From Dev

How to implement generic dictionary class?

From Dev

Unable to invoke method declare in class implement generic interface method

From Dev

Unable to invoke method declare in class implement generic interface method

From Dev

How to implement generic Function method parameters?

From Dev

How to implement a interface method with <S extends T> Iterable<S>

From Dev

How to implement NSCoding on a generic class in Swift?

From Dev

How to implement generic fields from abstract class?

From Dev

How to implement compareTo in a generic method that takes a generic array as argument?

From Dev

How do I implement a generic class that has an constraint for a generic interface?

From Dev

How to instantiate generic class and call a method

From Dev

how to write a java method with generic class type?

From Dev

How to use Class type Generic in a method

From Dev

How to create a method that takes a generic class as a parameter

From Dev

How to pass a class level generic to a method

From Dev

how to write a java method with generic class type?

From Dev

How to call a wildcarded method on a generic class?

From Dev

How to use override method in generic class in JAVA

From Dev

how to send Class<T> to a generic method

From Dev

How to implement the didSelectRow method of the pickerView class?

From Dev

In TypeScript how to force inherited class to implement a method

From Dev

How to implement class with virtual method on the fly

From Dev

How to implement the didSelectRow method of the pickerView class?

From Dev

How to Implement Extension Method in Abstract Class

From Dev

How implement method logout from another class?

Related Related

  1. 1

    How to just get the method in the implement class with a generic interface in Java

  2. 2

    How to implement custom iterable class in VBA

  3. 3

    How implement an interface Generic Method

  4. 4

    How to implement a generic method in java

  5. 5

    How implement an interface Generic Method

  6. 6

    How to implement generic dictionary class?

  7. 7

    Unable to invoke method declare in class implement generic interface method

  8. 8

    Unable to invoke method declare in class implement generic interface method

  9. 9

    How to implement generic Function method parameters?

  10. 10

    How to implement a interface method with <S extends T> Iterable<S>

  11. 11

    How to implement NSCoding on a generic class in Swift?

  12. 12

    How to implement generic fields from abstract class?

  13. 13

    How to implement compareTo in a generic method that takes a generic array as argument?

  14. 14

    How do I implement a generic class that has an constraint for a generic interface?

  15. 15

    How to instantiate generic class and call a method

  16. 16

    how to write a java method with generic class type?

  17. 17

    How to use Class type Generic in a method

  18. 18

    How to create a method that takes a generic class as a parameter

  19. 19

    How to pass a class level generic to a method

  20. 20

    how to write a java method with generic class type?

  21. 21

    How to call a wildcarded method on a generic class?

  22. 22

    How to use override method in generic class in JAVA

  23. 23

    how to send Class<T> to a generic method

  24. 24

    How to implement the didSelectRow method of the pickerView class?

  25. 25

    In TypeScript how to force inherited class to implement a method

  26. 26

    How to implement class with virtual method on the fly

  27. 27

    How to implement the didSelectRow method of the pickerView class?

  28. 28

    How to Implement Extension Method in Abstract Class

  29. 29

    How implement method logout from another class?

HotTag

Archive