Presentation is loading. Please wait.

Presentation is loading. Please wait.

Why not just use Arrays? Java ArrayLists.

Similar presentations


Presentation on theme: "Why not just use Arrays? Java ArrayLists."— Presentation transcript:

1 Why not just use Arrays? Java ArrayLists

2 … if only an array could …
shrink when you remove something. Tell you if it contains what you’re looking for instead of looping through to check each element. Grow when you need more room... That’s an ArrayList, not an array!

3 ArrayList is a class in the core Java library
That’s the key to surviving in Java... Knowing what’s available already created Ready to use It’s all in the API!

4 What can you do with an ArrayList?
Add new objects to the list Remove old objects from the list Ask the list if it contains a certain object Ask if the list is empty Find the index of a certain object Get the size of the list Get an object from a certain position in the list. A powerful collection of methods!

5 How do you do it? Make a list Put something in it
Here’s the crate, it’s small because it’s empty Make a list ArrayList crate = new ArrayList(); Put something in it Egg chickie = new Egg(); crate.add(chickie); Put something else in it Egg babyRobin = new Egg(); crate.add(babyRobin); It grows to hold the Egg It grows to hold another

6 Find out how many things are in the crate
int howBig = crate.size(); Find out if it contains the chickie boolean isHere = crate.contains(chickie) Find out where the chickie is in the crate int position = crate.indexOf(chickie) Is the crate empty? boolean empty = crate.isEmpty(); Take something out crate.remove(chickie); 2 true =0, like an array, you start counting at zero. false Only babyRobin is left

7 Important ArrayList points
An array needs to know its size and type when it’s created. Not an ArrayList It grows and shrinks as needed There are advantages, however, to declaring a type for an ArrayList other than the default of Object

8 Point 2 To put an object in an array, you must assign it to a specific location. Not an ArrayList Each time you add something, you just say “add” the ArrayList finds a place for it. (Though you can add to a specific location)

9 Point 3 Arrays are homogeneous. All objects in an array must be the same type. Not an ArrayList, it’s heterogeneous. It can hold any kind of object But... It can NOT hold simple variables like int and boolean. (they will be auto-wrapped into objects)

10 Typed ArrayLists You can declare an ArrayList to have a type
HIGHLY RECOMMENDED

11 How do you do it? Make a list Put something in it
Here’s the crate, it’s small because it’s empty Make a list ArrayList<Egg> crate = new ArrayList<Egg>(); Put something in it Egg chickie = new Egg(); crate.add(chickie); Put something else in it Egg babyRobin = new Egg(); crate.add(babyRobin); It grows to hold the Egg It grows to hold another

12 In Summary Use an ArrayList!!!!
Need a bunch of objects, but there’s no way to know how many? Doesn’t really matter what order they’re in? Want to be able to pull something out without having to do a search for it? Want it to shrink as things are removed? Use an ArrayList!!!! fin

13 True, Java autowraps the ints into Integers.
Questions Given ArrayList<Integer> Nums = new ArrayList<Integer>; What are the contents after: Nums.add(3); Nums.get(1); Nums.add(5); Nums.set(0,8); Nums.remove(0); Wait! I thought you said you can’t put primitive objects in an ArrayList! True, Java autowraps the ints into Integers.

14 Some ArrayList methods
ArrayList<String> stringList = new ArrayList<String>(); //Generic ArrayList to Store only String objects Putting an Item into ArrayList Second line will result in compilation error because this Java ArrayList will only allow String elements. stringList.add("Item"); //no error because we are storing String stringList.add(new Integer(2)); //compilation error Checking size of ArrayList Size of an ArrayList in Java is total number of elements currently stored in ArrayList. int size = stringList.size(); Checking Index of an Item in Java Arraylist You can use indexOf() method of ArrayList in Java to find out index of a particular object. int index = stringList.indexOf("Item"); //location of Item object in List Retrieving Item from arrayList in a loop Many a times we need to traverse on Java ArrayList and perform some operations on each retrieved item. for(String item: stringList){ System.out.println("retrieved element: " + item); }

15 More methods Checking ArrayList for an Item
Sometimes we need to check whether an element exists in ArrayList in Java or not for this purpose we can use contains () method of Java. contains() method takes type of object defined in ArrayList creation and returns true if this list contains the specified element Checking if ArrayList is Empty We can use isEmpty() method of Java ArrayList to check whether ArrayList is empty. isEmpty() method returns true if this ArrayList contains no elements. You can also use size() method of List to check if List is empty boolean result = stringList.isEmpty(); //isEmpty() will return true if List is empty if(stringList.size() == 0){    System.out.println("ArrayList is empty"); } Removing an Item from ArrayList There are two ways to remove any elements from ArrayList in Java. You can either remove an element based on its index or by providing object itself. Remove remove (int index) and remove (Object o) method is used to remove any element from ArrayList in Java. Since ArrayList allows duplicate its worth noting that remove (Object o) removes the first occurrence of the specified element from this list, if it is present. In below code first call will remove first element from ArrayList while second call will remove first occurrence of item from ArrayList in Java. stringList.remove(0);   stringList.remove(item);

16 replace Replacing an element at a particular index
You can use set (int index, E element) method of java ArrayList to replace any element from a particular index. Below code will replace first element of stringList from "Item" to "Item2". stringList.set(0,"Item2");

17 END


Download ppt "Why not just use Arrays? Java ArrayLists."

Similar presentations


Ads by Google