Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class

Slides:



Advertisements
Similar presentations
Linked Lists Linear collections.
Advertisements

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;
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 7 Object Oriented Programming in Java Advanced Topics Collection.
15-Jun-15 Lists in Java Part of the Collections Framework.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
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.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
12-Jul-15 Lists in Java Part of the Collections Framework.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
The Java Collections Framework (JCF) Introduction and review 1.
ArrayList, Multidimensional Arrays
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
List data type(ADT). Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion.
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
Fundamental of Java Programming
EKT472: Object Oriented Programming
Chapter 5: The List Abstract Data Type
Software Development Java Collections
Data Structures Lakshmish Ramaswamy.
ARRAYLIST AND VECTOR.
COP 3503 FALL 2012 Shayan Javed Lecture 8
Implementing ArrayList Part 1
Arrays and the ArrayList Class The ArrayList Class
Welcome to CSE 143!.
Java Collections Overview
Introduction to Collections
TCSS 143, Autumn 2004 Lecture Notes
ArrayLists.
Programming in Java Lecture 11: ArrayList
The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.
Data Type (how to view it)
Introduction to Collections
(Java Collections Framework) AbstractSequentialList
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.
Chapter 10 ArrayList reading: 10.1
Welcome to CSE 143!.
Collections in Java The objectives of this lecture are:
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Object-Oriented Programming Paradigm
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
Computer Science and Engineering
Introduction to Collections
ArrayLists 22-Feb-19.
Collections Framework
ArrayLists.
ArrayLists 27-Apr-19.
ArrayList.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Web Design & Development Lecture 6
Part of the Collections Framework
Arrays and ArrayLists.
Presentation transcript:

Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class Mouna KACEM mouna@cs.wisc.edu Spring 2019

Java ArrayList Class Java ArrayList Class in Java.util package uses a dynamic array for storing a set of elements inherits AbstractList class and implements List interface Main Properties Java ArrayList class can contain duplicate elements Java ArrayList class maintains insertion order Java ArrayList allows random access The dynamic array works at the index basis Iterable Collection List AbstractList ArrayList extends implements https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

Java ArrayList Class Constructors ArrayList list1 = new ArrayList(); //creating an empty ArrayList ArrayList<String> list2 = new ArrayList<String>(); //creating an empty ArrayList of elements of type String the type of elements is specified in angular braces ArrayList list2 is forced to have only specified type of objects in it (here String). Adding another type of object, results in a compile time error ArrayList(Collection c) build an array list that is initialized with the elements of the collection c. ArrayList(int capacity) build an array list that has the specified initial capacity.

Java ArrayList Class Main Methods (1) boolean add(Object o) used to append the specified element to the end of a list. void add(int index, Object element) insert the specified element at the specified position index in a list. boolean addAll(Collection c) append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. boolean addAll(int index, Collection c) used to insert all of the elements in the specified collection into this list, starting at the specified position.

Java ArrayList Class Main Methods (2) int indexOf(Object o) used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element. int lastIndexOf(Object o) return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.

Java ArrayList Class Main Methods(3) void clear() Object[] toArray() remove all of the elements from this list. Object[] toArray() used to return an array containing all of the elements in this list in the correct order. Object clone() used to return a shallow copy of an ArrayList.