Presentation is loading. Please wait.

Presentation is loading. Please wait.

ArrayList By Neil Butcher. What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle.

Similar presentations


Presentation on theme: "ArrayList By Neil Butcher. What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle."— Presentation transcript:

1 ArrayList By Neil Butcher

2 What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle differences. –Arrays are declared with a set length such as: int grades[]=new int[10]; – An ArrayList would be declared as: List grades=new ArrayList() –The ArrayList is declared with no set length, where a regular array has a set length.

3 Setting the Elements in Your ArrayList An ArrayList may a set type of object, it cannot store primitive data types unless cast as an object. –ArrayList grade=new ArrayList (); It is required for Java 5.0 that you include a data type, it may also be a class. –ArrayList people=new ArrayList (); This makes keeping track of the type stored in the ArrayList more simple and the program will run more efficiently.

4 Using Array List as Return Type In a Method public ArrayList getClassList() { // code here } An ArrayList can be returned to a method, this can be very useful when finding possible indexes They may also be a parameter for a method: public int getLocations(ArrayList locs) { //code here } These both can be very useful when writing a long and draw out class.

5 Types of Methods to Use on an ArrayList size() isEmpty() add(E elmt) add(int i, E elmt) -Returns the number of elements in an ArrayList ***Careful when using this method is returns the number of elements i.e. the length+1. -Returns a Boolean stating whether or not the ArrayList has anything in it. -Returns a Boolean and will add the element at the end of the list and return true. - Inserts elmt into i of ArrayList, shift everything else to the right.

6 get(int i) set(int I, E elmt) remove(int i) Returns the value in the i element of the ArrayList. Replaces the element in i of ArrayList with elmt Removes the element i in the ArrayList. **decrements size of ArrayList by 1

7 .get() Method ArrayList test=new ArrayList (); //test is set to{ 4, 10, 16, 3, 18} test.get(2); // will return 16 for(int x=0; x<test.size(); x++) System.out.println(test.get(x));// This will display: //4 //10 //16 //3 //18

8 .size() Method ArrayList test=new ArrayList (); //test is set to{ 4, 10, 16, 3, 18} int last=test.size();// last is set to the value of 5. //in this line it asks for the element at size test.get(test.size()); //it will return an out of bounds array ERROR. //proper decleration to get the last element in the array would look like //this: test.get(test.size()-1); The bottom method works because size returns the number of elements in the ArrayList not the length of it.

9 .add() Method ArrayList test=new ArrayList (); //test is set to{ 4, 10, 16, 3, 18} test.add(7); //test is now set to {4, 10, 16, 3, 18, 7} If you don’t use the method which clarifies the position of the object being inserted it will simply add it to the end of an array and increment the length of the ArrayList by one. test.add(3, 19); //test is now set to {4, 10, 16, 19, 3, 18, 7} This method will add the element at the specified index the first parameter of the method specifies where in the ArrayList the second parameter will go.

10 .remove() Method ArrayList test=new ArrayList (); //test is set to{ 4, 10, 10, 16, 3, 18, } test.remove(3); //sets test equal to{ 4, 10, 10, 3, 18} for(int x=0; x<test.length(); x++) { if(test.get(x)==10) test.remove(x); } This code will NOT work properly. The intention would be to remove all the elements of 10 in the list, but seeing how it changes the length of the array when it runs through the remove method it will not notice the second 10 and return an ArrayList with {4, 10, 16, 3, 18}

11 .set() Method The set method will take a position in an Array list and an element to replace the one at it currently ArrayList test=new ArrayList (); //test is set to{ 4, 10, 10, 16, 3, 18, } test.set(0, 14); //test is now set to{ 14, 10, 10, 16, 3, 18, } This method is fairly straightforward and simple, it is almost like using a remove and an add method at the same time. It will have occasional uses.

12 The End This was a quick and brief introduction to the ArrayList (); While this may have covered a very minimal part of what you can do with an ArrayList, you can try them out on BlueJ yourself and test and see the possibilities with an ArrayList.


Download ppt "ArrayList By Neil Butcher. What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle."

Similar presentations


Ads by Google