Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Lists Java Lists List Basics: Adding, Removing, and Accessing

Max Green
Max Green
12,517 Points

<> Information

How do the <> brackets at the creation of the list/arraylist work? I've tried fiddling with other types of data i.e. <int> <boolean> and they don't seem to work. Also, what's the reason for getting rid of the data type in the ArrayList side brackets? I really don't know the function of that little part at all lol

JESAMAE DAMASCO
JESAMAE DAMASCO
1,914 Points

That is for generic type in arraylist you are creating an array of non-primitive data types. Example: ArrayList<Integer> arr = new ArrayList<Integer>(); You can use also arr.add(new Integer(1)); add is one of the method that you can use to add value in array of Integer. And if you want to get the value you can do this. Integer i = list.get(1); And also (int) is different from (Integer) because <> in ArrayList is use only for non-primitive data types.

2 Answers

Ryan Colmon
PLUS
Ryan Colmon
Courses Plus Student 13,803 Points

The <> take an object between them, you can't use any of the 7 primitives types as objects (int, long, byte, float, double, char, boolean). There are class objects named after the primitives that you can use instead (Integer, Double, etc.).

As for removing the data type from the array list, the data type is not required if it is stated on the left side. You are free to leave it in though as it will still work. I think removing it is just to make the code cleaner, it has no effect on the code.

Check out the Generics tutorial, if you haven't yet, some of this is touched a little bit there.

Max Green
Max Green
12,517 Points

Ohhh okay. I guess I need to do some more research then on exactly what non-primitive data types means. I'll look into that, thanks.