Constrain arrayList

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

    #1

    Constrain arrayList

    how to constrain a collection so it takes only strings
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    I think as of Java5 or Java6, you can declare an ArrayList with a class or data type inside <> brackets, like:

    Code:
    ArrayList<String> myStringList = new ArrayList<String>();
    and this will restrict myStringList to holding Strings.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by Ganon11
      I think as of Java5 or Java6, you can declare an ArrayList with a class or data type inside <> brackets, like:

      Code:
      ArrayList<String> myStringList = new ArrayList<String>();
      and this will restrict myStringList to holding Strings.
      Yes that is correct. Without using 1.5 you'd have to write yuor own ArrayList class and put the restriction on the add method.

      Comment

      Working...