Arrays and the ArrayList Class The ArrayList Class

Slides:



Advertisements
Similar presentations
Classroom Bill of Rights
Advertisements

Review Generics and the ArrayList Class
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
The ArrayList Class and the enum Keyword
Two Dimensional Arrays and ArrayList
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;
Reagan/Clinton Compare/Contrast Essay. I. Reagan and Clinton both argue... but... A.Reagan argues B.Clinton argues.
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Array Lists Chapter 7.2 Pages Array Lists In Java, Arrays are an important structure for storing data. We will learn more about arrays later,
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
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.
ПОРТФОЛИО профессиональной деятельности Белово 2015 Таюшовой Натальи Борисовны Преподавателя дисциплин «Химия», «Биология»
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
Collections.
Chapter Topics Chapter 7 discusses the following main topics:
Strings, StringBuilder, and Character
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 8 Arrays, Strings and pointers
Intro to Collections.
2.2 Defining Classes Part 2 academy.zariba.com.
COMP 121 Week 9: ArrayList.
ARRAYLIST AND VECTOR.
COP 3503 FALL 2012 Shayan Javed Lecture 8
Arrays, Searching and Sorting
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Welcome to CSE 143!.
Array List Pepper.
Building Java Programs
ArrayLists.
Programming in Java Lecture 11: ArrayList
Building Java Programs
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
Building Java Programs
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
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
CMSC 202 ArrayList Aug 9, 2007.
Chapter 10 ArrayList reading: 10.1
ArrayLists.
List Implementations Chapter 9.
Welcome to CSE 143!.
Lecture 2: Implementing ArrayIntList reading:
Arrays and Collections
Dynamic Data Structures and Generics
Object Oriented Programming in java
Welcome to CSE 143! Go to pollev.com/cse143.
CMSC 202 ArrayList Aug 9, 2007.
ArrayLists 22-Feb-19.
Index Notation Sunday, 24 February 2019.
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayLists.
Dr. Sampath Jayarathna Cal Poly Pomona
ArrayLists 27-Apr-19.
Review: libraries and packages
Object Oriented Programming
Unit-1 Introduction to Java
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Presentation transcript:

Arrays and the ArrayList Class The ArrayList Class Chapter 8 Arrays and the ArrayList Class The ArrayList Class

Contents The ArrayList Class Creating and Using an ArrayList Object The ArrayList Class's toString method Removing an Item from an ArrayList Inserting an Item Replacing an Item Capacity Using a Cast Operator with the get Method Using ArrayList As a Generic Data Type

I. The ArrayList Class The ArrayList class can be used for storing and retrieving objects. An ArrayList object is similar to an array of objects, but offers many advantages over an array: An ArrayList object automatically expands as items are added to it. In addition to adding items to an ArrayList, we can remove items as well. An ArrayList object automatically shrinks as items are removed from it.

II. Creating and Using an ArrayList Object Creating an ArrayList object ArrayList nameList = new ArrayList(); Adding items to the ArrayList object nameList.add(“James”); nameList.add(“Catherine”); nameList.add(“Bill”); The items that are stored in an ArrayList have a corresponding index. The first item that is added to an ArrayList is stored at index 0. The next item that is added to an ArrayList is stored at index 1, and so forth.

II. Creating and Using an ArrayList Object To get the number of items stored in an ArrayList: using the size() method. The ArrayList class's get method returns the item stored at a specific index. We pass the index as an argument to the method.

III. The ArrayList Class's toString method The ArrayList class has a toString method that returns a string representing all of the items stored in an ArrayList object. ArrayList nameList = new ArrayList(); nameList.add(“James”); nameList.add(“Catherine”); nameList.add(“Bill”); System.out.println(nameList); [James, Catherine, Bill]

IV. Removing an Item from an ArrayList The ArrayList class has a remove method that removes an item at a specific index.

V. Inserting an Item

V. Inserting an Item The add method adds an item at the last position in an ArrayList object. nameList.add(“Marry”); The ArrayList class has an overloaded version of the add method that allows to add an item at a specific index. nameList.add(1, “Marry”);

VI. Replacing an Item The ArrayList class's set method can be used to replace an item at a specific index with another item. nameList.set(1, “Becky”);

ArrayList nameList = new ArrayList(100); VII. Capacity An ArrayList object has a capacity, which is the number of items it can store without having to increase its size. When an ArrayList object is created, using the no-argument constructor, it has an initial capacity of 10 items. We can specify a different starting by passing an int value to the constructor: ArrayList nameList = new ArrayList(100);

VIII. Using a Cast Operator with the get Method The get method returns a reference to the object stored at a specific index. We have to use a cast operator to convert the reference to the correct type manually. ArrayList nameList = new ArrayList(); nameList.add(“James”); String str = (String)nameList.get(0);

IX. Using ArrayList As a Generic Data Type We can specify the type of object that an ArrayList will store, and Java will make sure that only objects of the specified type are stored in it. ArrayList<String> name = new ArrayList<String>();