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 trialKenneth Milota
Courses Plus Student 178 PointsCan someone please just give me an example of how ArrayLists are used in types programs that most people use regularly?
I know what ArrayLists are. I know how to create them, I know how to add to them, I know how to delete and reorder them, how to convert them to strings, etc...
Having programmed for only about 15 weeks or so, I still don't understand what they really mean though. I keep going to YouTube or other videos showing ArrayList example, and most of them for some reason are obsessed with fruit. They all use an example showing how to make a list of your favorite fruits.
Okay, seriously? How many professional developers sit in their office and think about whether they like bananas or oranges better and how to add or delete them to a list?
Say for example a group such as Treehouse like this was developed with Java, even if it wasn't. What on this site uses an ArrayList to generate what we are seeing now? Say, I'm logging into a store such as Amazon. What part of their website application uses an ArrayList if they used Java to make it?
Thanks.
1 Answer
Kenneth Milota
Courses Plus Student 178 PointsWhy don't they just use a SQL database for that?
Ben Payne
1,464 PointsI'm sure there is a persistence layer of some kind. SQL, NoSql or some other kind of database. The ArrayList, or any other data structure, takes the relational data from the database and maps it into a structure that can be used as needed.
For example, let's say you had a make of a car; an Honda Accord for instance. There are several trims for that car, LE, SE, Sport Edition etc. In a database you would have a table for the Car, the Trims, and then a lookup table to map the Cars to the Trims. Well, when you need to display these in a browser, or in a client running on the machine, you need some way to programatically display any number of cars and there associated trims. So you would end with a Map (ArrayList, LinkedList etc) like so:
[
"Honda Accord" : [
"LE",
"SE",
"Sport Edition",
],
"Other Car": [
"Trim 1",
"Trim 2",
"Trim 3",
]
// n number cars
]
In this way you can have 1 or n + 1 cars in the ArrayList and your code does not have to change. It just pulls from the database, maps the DB query to this array, and your done. You just loop through the list and display the contents:
for (String[] carTrims : cars) {
for (String carTrim : carTrims) {
System.out.print(carTrim );
}
}
It's been awhile since I've used Java so forgive any syntax issues here.
Hopefully that helps.
-Ben
Ben Payne
1,464 PointsBen Payne
1,464 PointsHey Kenneth,
An ArrayList can be used for all kinds of things. I can't say for certain on Teamtreehouse but I would imagine that any time you are seeing a list of dynamic content (A list of comments on this post for instance), that content is being loaded into an ArrayList (or similar data structure), then being looped through and displayed. Or on amazon, if you search for Movies, you'll get a list of movies that match that criteria. Again, I can't be sure, but i'm betting that the list is loaded into some type of array or array like data structure, then looped through and displayed.
So, yeah the fruit example is a bit contrived, but when you are working with Arrays, or Maps, or Linked Lists or whatever, just think of them as ways to store data, that can then be looped through and handled accordingly.
Hopefully that helps.
-Ben