Arrays and ArrayLists.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Linked Lists Linear collections.
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
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.
CS 106 Introduction to Computer Science I 04 / 30 / 2007 Last lecture :( Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Arrays And ArrayLists - S. Kelly-Bootle
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
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.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
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.
ArrayList By Neil Butcher. What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
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.
COMP More About Arrays Yi Hong June 05, 2015.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
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
EKT472: Object Oriented Programming
Lecture 10 Collections Richard Gesick.
(like an array on steroids)
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
ARRAYLIST AND VECTOR.
COP 3503 FALL 2012 Shayan Javed Lecture 8
Implementing ArrayList Part 1
Welcome to CSE 143!.
Building Java Programs
CS313D: Advanced Programming Language
TCSS 143, Autumn 2004 Lecture Notes
ArrayLists.
Programming in Java Lecture 11: ArrayList
ArrayLists.
Arrays versus ArrayList
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Java Programming Arrays
ArrayList Collections.
Welcome to CSE 143!.
Object Oriented Programming in java
Arrays .
ArrayLists 22-Feb-19.
Collections Framework
Dynamic Data Structures and Generics
Introduction to Data Structure
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Java SE 7 One and Multi Dimensional Arrays Module 6
ArrayLists.
ArrayLists 27-Apr-19.
Review: libraries and packages
ArrayList.
Part of the Collections Framework
ArrayLists Readings: 10.1 (pg. 559 – 571).
Why not just use Arrays? Java ArrayLists.
Presentation transcript:

Arrays and ArrayLists

Array Before we have only looked at single variables. An array can hold a number of variables. int[] anIntArray; // declares an array of integers anIntArray = new int[10]; // allocates memory for 10 integers Can hold primitive data types and objects.

Length of an array To the the “size” of an array use .length Is this a method? Useful when iterating through an array for(int i = 0; i < anIntArray.length; i++) { System.out.println("Element at index "+i+": " + anIntArray[i]); } Common error “Out of bounds exception”.

Arrays – benefits Arrays are good when you know how many elements you will have to deal with. However, if the number of elements is not known, or may change we have to manage an array. Deleting elements in an array and shuffling elements can be tricky and error prone. Sorted/Unsorted Arrays.

ArrayList An ArrayList can be thought of as a variable length array. Elements/items can be added and deleted, and inserted at a given position. import java.util.ArrayList;

Declare an arrayList ArrayList<String> stringList = new ArrayList<String>(); //note that we must state the data type of the variables we are going to store. In this case the data type is String

Adding an element Here we add two strings stringList.add("John"); stringList.add("David"); Note that we cannot add something of a different data type – we will get a compile time error

add(int index, E element) Inserts the specified element at the specified position in this list. It also shifts all elements up by one position And of course the size of the ArrayList has increased by one.

Contains We can ask if the arrayList stringList contains a given String The method will return true or false E.g. stringList.contains("John")

Is it Empty We could check if an arrayList is empty like this if(stringList.size() == 0){    System.out.println("ArrayList is empty"); } isEmpty() method returns true if this ArrayList contains no elements.

Removing an Item at an Index stringList.remove(0);  This will remove the item at position/index 0 It will also shuffle all the elements down by one position. So the arrayList is now shorter by one elements Check this with the size() method.

Replacing an element  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 with “AAA". stringList.set(0,“AAA"); Set index 0 to “AAA”

Clearing an ArrayList To “reset” an ArrayList just call the method This will remove all the data, and set the size to zero Confirm by calling method size().

indexOf and lastIndexOf Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. lastIndexOf Returns the index of the last occurrence of the specified element in this list,

addAll So far we have only added/deleted single elements addAll(Collection<? extends E> c) Appends 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. addAll(int index, Collection<? extends E> c) Inserts all of the elements in the specified collection into this list, starting at the specified position.

removeRange removeRange(int fromIndex, int toIndex) Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. BE CAREFUL WITH “off by one errors”!!! We (computer scientists) start counting at zero (most computer languages), which means the last elements of ArrayList size n is n-1, and the first (1st) has index zero.