Arrays An array is a collection of variables, all of the same type. An array is created with the new operator. The size of an array must be specified at.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chapter 21 Implementing lists: array implementation.
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
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;
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
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.
CS 2430 Day 14. Agenda for today The Bag class No mixing! What if we have a bunch of String s that we want to store in our program? We can’t use the.
From Theory to Practice 2 OOP Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Implementation options There are two basic approaches: –array-based –linked We will consider array-based implementation first Use a TDD approach: first.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Chapter 3 Collection Classes Anshuman Razdan Department of Engineering
CS 106 Introduction to Computer Science I 05 / 03 / 2010 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.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
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.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
Chapter 3 Introduction to Collections – Stacks Modified
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
Data structures Abstract data types Java classes for Data structures and ADTs.
Advanced Java Programming CS 537 – Data Structures and Algorithms.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
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.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
CS 2430 Day 12. Agenda for today Container class example: DateList Growable container classes.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
Arrays. Array: Sequence of values of the same type Construct array: Store in variable of type double[ ] new double[10] double[] data = new double[10];
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
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
Implementing ArrayList Part 1
TCSS 143, Autumn 2004 Lecture Notes
null, true, and false are also reserved.
Programming in Java Lecture 11: ArrayList
slides created by Ethan Apter
slides created by Ethan Apter
ArrayLists 22-Feb-19.
Collections Framework
COMPUTER 2430 Object Oriented Programming and Data Structures I
slides created by Ethan Apter and Marty Stepp
CHAPTER 3: Collections—Stacks
Presentation transcript:

Arrays An array is a collection of variables, all of the same type. An array is created with the new operator. The size of an array must be specified at creation time. The memory for an array is allocated in a contiguous block. The size of an array is fixed.

Array indexing A variable in an array is accessed using special syntax. –Suppose Object[] x = new Object[4]; –Variables: x[0], x[1], x[2] and x[3] –Index ranges from 0 to size-1.

Generics You’ve seen how to use generic collections in CSE115. We’ll discuss later how to define them – don’t sweat these details now.

Collection interface We’ll set up our Bag implementation to implement the java.util.Collection interface. Method signatures come from this interface. We will not look at all the required methods today, only the four identified earlier: –add, remove, contains and size

public class Bag implements Collection { private final static int DEFAULT_CAPACITY = 10; private E[] _store; private int _size; public Bag() { this(DEFAULT_CAPACITY); } public Bag(int initialCapacity) { if (initialCapacity < 1) { initialCapacity = DEFAULT_CAPACITY; } _store = (E[]) new Object[initialCapacity]; _size = 0; }

public boolean add(E obj) { if (_size == _store.length) { resize(); } _store[_size] = obj; _size++; return true; }

public boolean remove(Object obj) { for (int i = 0; i < _size; i++) { if (_store[i] == obj) { _size--; _store[i] = _store[_size]; _store[_size] = null; return true; } return false; }

public boolean contains(Object obj) { for (int i = 0; i < _size; i++) { if (_store[i] == obj) { return true; } return false; }

public int size() { return _size; }