Why not just use Arrays? Java ArrayLists.

Slides:



Advertisements
Similar presentations
Review Generics and the ArrayList Class
Advertisements

DATA STRUCTURES USING C++ Chapter 5
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Generics, Lists, Interfaces
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Programming with Collections Collections in Java Using Arrays Week 9.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Chapter 18 Java Collections Framework
The Java Collections Framework By the end of this lecture you should be able to: use the ArrayList class to store a list of objects; use the HashSet class.
Grouping objects Introduction to collections 5.0.
Built-in Data Structures in Python An Introduction.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSE 1201 Object Oriented Programming ArrayList 1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Arrays. Array: Sequence of values of the same type Construct array: Store in variable of type double[ ] new double[10] double[] data = new double[10];
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
COMP More About Arrays Yi Hong June 05, 2015.
Python Programing: An Introduction to Computer Science
JAC444: Intro to Java Arrays and Vectors Tim McKenna
Collections Dwight Deugo Nesa Matic
The ArrayList Data Structure The Most Important Things to Review.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
The ArrayList Data Structure Standard Arrays at High Speed!
Chapter 9 Introduction to Arrays Fundamentals of Java.
Click to edit Master text styles Stacks Data Structure.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
CMSC 202 ArrayList Aug 9, 2007.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays versus ArrayList
CMSC 202 ArrayList Aug 9, 2007.
Object Oriented Programming in java
CMSC 202 ArrayList Aug 9, 2007.
ArrayLists 22-Feb-19.
Review: libraries and packages
Arrays and ArrayLists.
Why not just use Arrays? Java ArrayLists.
Presentation transcript:

Why not just use Arrays? Java ArrayLists

… 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!

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!

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!

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

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

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

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)

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)

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

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

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

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.

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); }

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);

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");

END