ArrayList Type


Premium Content - Free Preview

Previously, you saw that an ArrayList could be created with a simple line of code:

ArrayList someList = new ArrayList();

This works to create an ArrayList, but there's one issue: it doesn't state what Class of Objects it will hold. In most cases you'll want an ArrayList that only holds Objects of one Class, so your program will know what it's getting when it accesses an element in it. To specify the Objects that an ArrayList will hold, you use the Angle Brackets syntax. For example: *

ArrayList<String> someList = new ArrayList<String>();

This code will create an ArrayList of String, which will only hold String Objects.


End of Free Content Preview. Please Sign in or Sign up to buy premium content.

Comments

  • Can't get this to work:

    import java.util.ArrayList;

    public class Notebook
    {

    private ArrayList notes;

    public Notebook()
    
    cont...
  • Sorted!

  • "Modify the ArrayList notes in the Notebook class so that it only takes in String Objects."

    Is it just me, or do you actually want the ArrayList notes to be taking Note Objects rather than String Objects...?

  • @Jason, thanks, corrected.

Contact Us
Sign in or email us at [email protected]