ArrayLists.

Slides:



Advertisements
Similar presentations
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
Advertisements

CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
25-Jun-15 Vectors. 2 Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: Arrays have special syntax; Vector.
Vectors. Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: –Arrays have special syntax; Vector s don’t.
Building Java Programs
1 CSE 143 Lecture 1: ArrayList reading: Welcome to CSE 143! I'm Allison Obourn
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
Copyright 2008 by Pearson Education Building Java Programs Chapter 10 Lecture 10-1: ArrayList reading: 10.1.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
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.
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
Copyright 2010 by Pearson Education Building Java Programs Chapter 10 Lecture 21: ArrayList reading: 10.1.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Building Java Programs Chapter 10 ArrayList Copyright (c) Pearson All rights reserved.
EKT472: Object Oriented Programming
Using the Java Collection Libraries COMP 103 # T2
slides created by Marty Stepp
Collections.
Collections.
COP 3503 FALL 2012 Shayan Javed Lecture 8
Building Java Programs Chapter 14
Welcome to CSE 143!.
Array List Pepper.
Building Java Programs
Building Java Programs Chapter 10
TCSS 143, Autumn 2004 Lecture Notes
Lecture 2: Implementing ArrayIntList reading:
Programming in Java Lecture 11: ArrayList
Building Java Programs
Building Java Programs
CSE 143 Lecture 4 ArrayList Reading: 10.1.
ArrayLists.
Building Java Programs Chapter 10
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Building Java Programs
Topic 33 ArrayList.
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Chapter 10 ArrayList reading: 10.1
Welcome to CSE 143!.
Lecture 1: ArrayList reading: 10.1
Lecture 2: Implementing ArrayIntList reading:
Welcome to CSE 143! Go to pollev.com/cse143.
Building Java Programs
Grouped Data Arrays, and Array Lists.
Lecture 13: ArrayLists AP Computer Science Principles
ArrayLists 22-Feb-19.
CSE 143 Lecture 4 Implementing ArrayIntList
CSE 143 Lecture 2 More ArrayList; classes and objects
Lecture 16: Arraylist Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
CSE 143 Lecture 3 Implementing ArrayIntList reading:
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
ArrayLists.
Parsing JSON, Using Libraries, Java Collections, Generics
slides created by Marty Stepp
Building Java Programs
ArrayLists 27-Apr-19.
ArrayList.
slides created by Marty Stepp
CSE 143 Lecture 4 Implementing ArrayIntList reading:
CSE 373 Java Collection Framework reading: Weiss Ch. 3, 4.8
TCSS 143, Autumn 2004 Lecture Notes
Arrays and ArrayLists.
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Implementing ArrayIntList
Presentation transcript:

ArrayLists

Similarity between ArrayList and Array A ArrayList is like an array of Objects Differences between ArrayList and Arrays Arrays have special convenience syntax; ArrayLists don’t An array is a fixed size, but an ArrayList can grow and shrink as you add and remove objects. This means you do not need to know the size beforehand Arrays can hold primitives or objects. ArrayLists can only hold objects

Creating a ArrayList The Object type must be specified when creating an ArrayList. Syntax: Specify, in angle brackets after the name, the type of object that the class will hold. Examples: ArrayList<String> strList1 = new ArrayList<String>(); ArrayList<String> strList2 = new ArrayList<String>(10); ArrayList<Card> deckOfCards = new ArrayList<Card>(); ArrayList<Integer> ageList = new ArrayList<Integer>(); * Note the Integer wrapper class was used in place of a primitive data type.

Adding elements to an ArrayList Elements can be added to the front, back, or elsewhere.

Adding elements to an ArrayList boolean add(Object obj) Appends the object obj to the end of this ArrayList With generics, the obj must be of the correct type, or you get a compile- time (syntax) error Always returns true This is for consistency with other, similar classes void add(int index, Object element) Inserts the element at position index in this ArrayList The index must be greater than or equal to zero and less than or equal to the number of elements in the ArrayList

Removing elements from an ArrayList boolean remove(Object obj) Removes the first occurrence of obj from this ArrayList Returns true if an element was removed Uses equals to test if it has found the correct element void remove(int index) Removes the element at position index from this ArrayList void clear() Removes all elements

Accessing elements stored in an ArrayList arrayListName.get(indexNumber) Returns the object at position index Example ArrayList<String> myFruit= new ArrayList<String>(); myList.add("GrapeFruit"); myList.add("Banana"); System.out.println(myFruit.get(1)); Output: Banana

Searching a ArrayList boolean contains(Object element) Tests if element is a component of this ArrayList Uses equals to test if it has found the correct element int indexOf(Object element) Returns the index of the first occurrence of element in this ArrayList Returns -1 if element was not found in this ArrayList int lastIndexOf(Object element) Returns the index of the last occurrence of element in this ArrayList

Additional Functionality of an ArrayList boolean isEmpty() Returns true if this ArrayList has no elements int size() Returns the number of elements currently in this ArrayList Object[ ] toArray() Returns an array containing all the elements of this ArrayList in the correct order

Most Common ArrayList methods: add(value) appends value at end of list add(index, value) inserts given value just before the given index, shifting subsequent values to the right clear() removes all elements of the list indexOf(value) returns first index where given value is found in list (-1 if not found) get(index) returns the value at given index remove(index) removes/returns value at given index, shifting subsequent values to the left set(index, value) replaces value at given index with given value size() returns the number of elements in list toString() returns a string representation of the list such as "[3, 42, -7, 15]"

Additional ArrayList methods: addAll(list) addAll(index, list) adds all elements from the given list to this list (at the end of the list, or inserts them at the given index) contains(value) returns true if given value is found somewhere in this list containsAll(list) returns true if this list contains every element from given list equals(list) returns true if given other list contains the same elements iterator() listIterator() returns an object used to examine the contents of the list (seen later) lastIndexOf(value) returns last index value is found in list (-1 if not found) remove(value) finds and removes the given value from this list removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given list from this list subList(from, to) returns the sub-portion of the list between indexes from (inclusive) and to (exclusive) toArray() returns the elements in this list as an array

Considerations for the equals method There are many different notions of equality Example 1: Two lists are equal if they contain the same elements; order of elements is irrelevant Example 2: Two lists are equal if they contain the same elements in the same order. Java defines public boolean equals(Object) in the Object class, but equals is defined to be the same as == It’s often a good idea to override equals for your own objects If you do this, note that the argument should be a general Object The String class (and some others) override equals

Conclusion A ArrayList is like an array of Objects The advantage of a ArrayList is that you don’t need to know beforehand how big to make it The disadvantage of a ArrayList is that you can’t use the special syntax for arrays You should never use an array that you hope is “big enough”—use an ArrayList instead

Exercise Write a program that reads a paragraph and displays the individual words as a list. First display all words. Then display them in reverse order. Then display them with all plurals (ending in "s") capitalized. Then display them with all plural words removed.

Partial Solution ArrayList<String> allWords = new ArrayList<String>(); Scanner in = new Scanner(System.in); String paragraph = in.nextLine(); Scanner input = new Scanner(paragraph); while (input.hasNext()) { String word = input.next(); allWords.add(word); } System.out.println(allWords); // remove all plural words for (int i = 0; i < allWords.size(); i++) { String word = allWords.get(i); if (word.endsWith("s")) { allWords.remove(i); i--;