Lesson 24 Arraylist Mimi Duong Rosalba Rodriguez Java Crash Course January 6th, 2015
Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Storing Items in Java How have we been storing our information recently? storing items inside instance variables, and calling on it when we need it for example: int x = 5; Systemoutprintln(x); // prints 5 So what happens if we have multiple items of the same type that we want to store? Say we want to have and use 15 integer values? Would we make 15 different variables, one at a time?
Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Data Structures A data structure is a way to store multiple things of the same type in Java examples from real life: grocery lists, class schedule, schedule for the day, to do list, etc
Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Data Structures Just as we had examples in real life of data structures, there are data structures in Java Although there are many different data structures in Java, right now we are only going to focus on ArrayLists
Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork ArrayList Think of ArrayList as just a regular list The same thing you do to lists in real life, you can do to and ArrayList in Java add an item remove an item change the order of items *Remember that when you make an ArrayList in Java, you have to store the same type of items inside of this ArrayList In addition to this, know that this is a premade class inside of Java So similar to Scanners, we will have to import the class the file we are using ArrayLists in order to use it properly We do so by the following: import javautilarraylist;
Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork ArrayList General syntax for declaring an ArrayList: ArrayList<TypeOfList> name = new ArrayList<TypeOfList>();
Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Live Coding Follow along with the instructor on the creating ArrayLists!
Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Live Coding Review Some things to take into consideration when using ArrayLists: Index of an ArrayList starts at 0 So if the size of your ArrayList is 1, then the index of that one object is 0! If the size of your ArrayList is 10, then the index of the last object is 9 and the index of the first object is 0 You need to import Arraylists before you can use them ArrayLists can be as big or as small as you want They are a type of a data structure (used to hold objects) An ArrayList will only hold the same object, where you tell Arraylist what it holds when you create and instantiate it
Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Methods for ArrayList Just as we can do different things to lists that we have in real life, we can do similar things to the ArrayLists that we have in Java add(object i) - adds the object between the paranthesis into the end of the ArrayList remove(int i) - removes the object that is located at the index i inside the ArrayList size() - returns how many objects are inside of the ArrayList get(int i) - returns the object that is located at the index i inside the ArrayList contains(object i) - returns true or false depending on whether or not object i exists inside the ArrayList As always, you can find additional methods ArrayLists have to use inside the ArrayList API by CLICKING HERE
Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Making the Program Look Through the ArrayList Why would we do this in the first place? The list is stored in the computer s memory we won t remember it all (imagine lists of thousands of objects) To print it onto the screen for whatever purposes is necessary ie such as a teacher printing out all the grades in a class To search through the list for a specific object in order to do something to it To see if there are any single object that exists in two specific ArrayLists
Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Relating to for-loops This is where for-loops come in handy because We can loop through all the indexes for ArrayLists Starting at index 0, we can keep increasing the index until we get to the last object of the ArrayList! Follow along with the instructor on how to do so
Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Classwork 10 Classwork 10, otherwise known as Homework 2 Extra Credit Because the requests for this classwork is slightly wrong, please READ ABOUT IT HERE Make sure you download the SKELETON CODE HERE As always, break the problem into smaller pieces