constrained type parameters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    constrained type parameters

    cd u give me the example of constrained type parameters
  • oll3i
    Contributor
    • Mar 2007
    • 679

    #2
    is it something like
    Code:
    import java.util.List;
    import java.util.ArrayList;
    
    public class Library<E extends Media> {
        private List<E> resources = new ArrayList<E>();
        public void addMedia(E x) {
            resources.add(x);
        }
        public E retrieveLast() {
            int size = resources.size();
            if (size > 0) {
                return resources.get(size - 1);
            }
            return null;
        }
    }

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      or sth like this
      Code:
      int myProperty;
          public int getMyProperty() {
              return myProperty;
          }
          public void setMyProperty(int newValue) throws PropertyVetoException {
              try {
                  vceListeners.fireVetoableChange(
                      "myProperty", new Integer(myProperty), new Integer(newValue));
                  myProperty = newValue;
              } catch (PropertyVetoException e) {
                  throw e;
              }
          }
          
          // Create the listener list.
          VetoableChangeSupport vceListeners = new VetoableChangeSupport(this);
          
          // The listener list wrapper methods.
          public synchronized void addVetoableChangeListener(VetoableChangeListener listener) {
              vceListeners.addVetoableChangeListener(listener);
          }
          public synchronized void removeVetoableChangeListener(VetoableChangeListener listener) {
              vceListeners.removeVetoableChangeListener(listener);
          }

      Comment

      • oll3i
        Contributor
        • Mar 2007
        • 679

        #4
        how to do it with ArrayList<Strin g>

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by oll3i
          cd u give me the example of constrained type parameters
          If you want to restrict a collection to store object of type E only then you declare as

          Code:
           ArrayList<E> list = new ArrayList<E>();

          Comment

          Working...