ARRAYLIST AND VECTOR.

Slides:



Advertisements
Similar presentations
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Two Dimensional Arrays and ArrayList
Generics, Lists, Interfaces
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
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.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
EKT472: Object Oriented Programming
CMSC 202 ArrayList Aug 9, 2007.
Chapter 7 – Arrays and Array Lists
Sixth Lecture ArrayList Abstract Class and Interface
Collections.
Objects First with Java Introduction to collections
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 20 Generic Classes and Methods
Intro to Collections.
COP 3503 FALL 2012 Shayan Javed Lecture 8
Arrays, Searching and Sorting
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Arrays and the ArrayList Class The ArrayList Class
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Continuing Chapter 11 Inheritance and Polymorphism
Welcome to CSE 143!.
Array List Pepper.
TCSS 143, Autumn 2004 Lecture Notes
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.
Chapter 8: Advanced Arrays and the ArrayList Class
ArrayLists.
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 8 Slides from GaddisText
CMSC 202 ArrayList Aug 9, 2007.
Chapter 10 ArrayList reading: 10.1
ArrayLists.
And the list goes on and on and on….
Data Structures and Database Applications Arrays And Lists
Welcome to CSE 143!.
Dynamic Data Structures and Generics
14.1 The java.util Package.
Object Oriented Programming in java
CMSC 202 ArrayList Aug 9, 2007.
Collections Not in our text.
ArrayLists 22-Feb-19.
Review of Previous Lesson
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 2 : List ADT Part I – Sequential List
Review: libraries and packages
Arrays and ArrayLists.
Arrays.
Presentation transcript:

ARRAYLIST AND VECTOR

List Class ArrayList and class Vector belong to List interface. List is sub class of Collections. Collections Class provides static methods that manipulate collections polymorphically. A List (sometimes known as sequence) is an ordered Collection that can contain duplicate elements. List indices are zero based. Class ArrayList and Vector are resizable-array implementation of List.

With various methods available in the ArrayList class, empty list can be created, elements can be added, removed, accessed and we can know the size of the list. Similarly with Vector class, various methods are available to create list and manipulate it as necessary

Elements stored in ArrayList and Vector are objects of a subclass, whose superclass is Object class. Elements loose all type information. Thus, before elements can be used, they must be cast first to a correct type

Autoboxing occurs when primitive-type values are inserted into the list of type ArrayList or Vector ArrayList and Vector classes are located in java.util. Hence they must first be imported before the associated classes can be utilized in the Java program. To import ArrayList use import java.util.ArrayList;. To import class Vector use this; import java.util.Vector; or we can use import java.util.*;.

The ArrayList Class Similar to an array, an ArrayList allows object storage Unlike an array, an ArrayList object: Automatically expands when a new item is added Automatically shrinks when items are removed Requires: import java.util.ArrayList;

Creating and Using an ArrayList Create an ArrayList object with no-args constructor ArrayList nameList = new ArrayList(); To populate the ArrayList, use the add method: nameList.add("James"); nameList.add("Catherine"); To get the current size, call the size method nameList.size(); // returns 2

Creating and Using an ArrayList To access items in an ArrayList, use the get method nameList.get(1); In this statement 1 is the index of the item to get. Example: ArrayListDemo1.java

Using an ArrayList The ArrayList class's toString method returns a string representing all items in the ArrayList System.out.println(nameList); This statement yields : [ James, Catherine ] The ArrayList class's remove method removes designated item from the ArrayList nameList.remove(1); This statement removes the second item. See example: ArrayListDemo3.java

Using an ArrayList The ArrayList class's add method with one argument adds new items to the end of the ArrayList To insert items at a location of choice, use the add method with two arguments: nameList.add(1, "Mary"); This statement inserts the String "Mary" at index 1 To replace an existing item, use the set method: nameList.set(1, "Becky"); This statement replaces “Mary” with “Becky” See example: ArrayListDemo4.java

Using an ArrayList An ArrayList has a capacity, which is the number of items it can hold without increasing its size. The default capacity of an ArrayList is 10 items. To designate a different capacity, use a parameterized constructor: ArrayList list = new ArrayList(100);

Using a Cast Operator with the get Method An ArrayList object is not typed To retrieve items from an ArrayList, you must cast the item to the appropriate type ArrayList nameList = new ArrayList(); nameList.add("Mary"); // Inserts an item String str = (String)nameList.get(0); Try get without the cast to see the effect. Example: ArrayListDemo6.java

Using ArrayList as a Generic Data Type You can create a type-safe ArrayList object by using generics. For example an ArrayList object for Strings: ArrayList<String> nameList = new ArrayList<String>(); The get method no longer requires casts to work. Example: GenericArrayListDemo1.java Example: GenericArrayListDemo2.java