Objects and Collections of Objects COMP53 Aug 31 2007.

Slides:



Advertisements
Similar presentations
Data Structures1 Basic Data Structures Elementary Structures Arrays Lists Search Structures Binary search Tree Hash Tables Sequence Structures Stacks Queues.
Advertisements

Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
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
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Lecture 7: Arrays Yoni Fridman 7/9/01 7/9/01. OutlineOutline ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
CSE 1301 J Lecture 13 Sorting Richard Gesick. CSE 1301 J 2 of 30 Sorting an Array When an array's elements are in random order, our Sequential Search.
Pointer Data Type and Pointer Variables
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
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.
Problem Solving for Programming Session 8 Static Data Structures.
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Working with arrays (we will use an array of double as example)
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Object-Oriented Programming in C++
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – Week 8 Linked List (a reference based.
Data Structures - Part I CS 215 Lecture 7. Motivation  Real programs use abstractions like lists, trees, stacks, and queues.  The data associated with.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
1 CS161 Introduction to Computer Science Topic #15.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
The Selection Sort Mr. Dave Clausen La Cañada High School.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Windows Programming Lecture 03. Pointers and Arrays.
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Mr. Dave Clausen La Cañada High School
Parameter Passing in Java
Java Programming Language
Presentation transcript:

Objects and Collections of Objects COMP53 Aug

Classes and Objects In Java, (almost) everything is either a class or an object. A class defines a type, which describes how to create and use objects which are instances of that class. Class members (non-static): – Instance variable or fields – Methods

Static Class Members Classes may also define static members, one copy of which is shared by all instances of the class This makes the class an object itself, which holds the static/shared members Static members can be accessed and used even when no instances of the class exist

References Java is a referenced based language. This means that names do not refer directly to objects. Names are indirect references to objects. This distinction will be very important as we start building data structures.

// george is the name of a reference to a Chimpanzee Chimpanzee george; // create a Chimpanzee object and set george // to refer to that object. george = new Chimpanzee(); // galen is now a reference to the same object Chimpanzee galen = george; References: Example george Chimpanzee object george Chimpanzee object galen

Collections We often need to manage a large number of objects We do this by creating collection objects, which are structures that can hold multiple values or references. Most collection classes maintain special rules on how the collection is stored and accessed. For example: a queue collection only allows you to access the value that has been in the collection the longest (the head of the queue).

Arrays A common collection is the array An array is an indexed sequence of values The indexes are integers, beginning with zero

// a is a reference to an array of integers int[ ] a; // the array object has space to hold 5 integers a = new int[5]; // the first element of the array now holds a 5 a[0] = 5; // the fourth element of the array now holds a 7 a[3] = 5; Array of Integers a a a 5 a 57

To store objects, we can use an array of object references Creating the array only creates the references, objects must be individually created. Chimpanzee[ ] c;// a is a reference to an array of Chimpanzee references a = new Chimpanzee[5];// the array has space to hold 5 Chimpanzee references // note: We still have not created any Chimpanzees Storing Objects c c

After the array is created, then we can add object references to it c[0] = new Chimpanzee(“George”, 180.0); c[0] = new Chimpanzee(“Galen”, 150.0); Storing Objects (continued) c “George” c “Galen” 150.0

When moving object references, take care to not lose any of the objects. Suppose we want to swap the objects references by x and y: We might try: y = x; x = y; But then we’ll lose Object 2. Juggling References xy Object 1Object 2 xy Object 1Object 2

Use a temporary reference to hold references when necessary: temp = y; y = x; x = temp; Juggling References (cont.) xy Obj1Obj2 tempxy Obj1Obj2 temp xy Obj1Obj2 temp xy Obj1Obj2 temp

When moving references in an array (or other data structure) we also need temporary references // swap second and third objects in array a temp = a[2]; a[2] = a[1]; a[1] = temp; Moving References in an Array