Introducing Lists2:29 with Ben Deitch
In this video we'll talk about where Lists came from and what types of problems they solve!
[MUSIC] 0:00 In the past few courses we learned about inheritance, interfaces, and generics. 0:09 And when Java launched in 1995, that was the end of the story. 0:14 However, it didn't take long before developers wanted more. 0:19 One place Java seemed to be lacking was with arrays. 0:23 At this point, if we have a collection of objects, 0:26 the only way we can store them is with an array. 0:29 And arrays work great for things that have a fixed size, 0:33 like the number of months in a year or number of squares on a chess board. 0:36 But arrays don't work so well when the size of the collection changes. 0:40 For example, let's look at the checkout line at the grocery. 0:44 Right now, there's only a couple of people in line. 0:47 But if we jump forward to the holidays, there's a ton of people here. 0:50 So if we wanted to use an array to store all the people in line we'd need to make 0:54 the array big enough to handle the holidays. 0:59 Which means most of the year, our array would just be taking up space. 1:02 It'd be as if the grocery left their queues up all year. 1:06 To fix this problem, in 1998 we got the Java Collections framework. 1:11 A set of classes and interfaces which give us a lot 1:16 more flexibility in how we store collections of objects. 1:19 Now the Java Collections framework is made up of a lot of classes and interfaces. 1:23 But for the most part, 1:28 there's only two types of collection that we'll use frequently, lists and maps. 1:29 We'll save maps for a later of course but right now, let's focus on lists. 1:36 In Java, a list is pretty much just a more flexible array. 1:41 Like an array, the items are indexed and we can access them by their index. 1:46 But unlike an array, with a list we can add or remove items anywhere we like. 1:50 We also get a few helpful methods, like contains() which returns a Boolean telling 1:56 us if a list contains a certain item. 2:01 And indexOf() which searches the list for an item and 2:03 returns the index of that item, if it's found, or negative one if it's not. 2:07 In addition to being a more flexible version of arrays, 2:13 lists also give us a bunch of methods to help deal with groups of objects. 2:16 In fact, in most cases you'll probably end up using a list instead of an array. 2:20 Let's see how to use a list in the next video. 2:26
You need to sign up for Treehouse in order to download course files.Sign up