Arrays of Objects 1 Fall 2012 CS2302: Programming Principles.

Slides:



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

Data Structures ADT List
Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
Exercise 1 Suppose C is a class that implements interfaces I and J. Which of the following Requires a type cast? C c = ……? I i = …..? J j = …..? c =
Wrappers: Java’s Wrapper Classes for the Primitives Types Steve Bossie.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Chapter 13 ARRAY LISTS AND ARRAYS CHAPTER GOALS –To become familiar with using array lists to collect objects –To learn about common array algorithms –To.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Chapter 7 – Arrays.
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Java Syntax Primitive data types Operators Control statements.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus 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.
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
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.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
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.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Recitation 7 Collections. Array List and Linked List Array List and Linked List are implementations of the same interface: List. As a result, they have.
ArrayLists (and the for-each loop) ArrayList example = new ArrayList (); example.add(4);
CSE 1201 Object Oriented Programming ArrayList 1.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Chapter 6 Arrays 1 Fall 2012 CS2302: Programming Principles.
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Objectives  File I/O: using Scanner with File  Inserting into Partially Filled Array  Deleting from Partially Filled Array  Static methods and variables.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Section 2.2 The StringLog ADT Specification. 2.2 The StringLog ADT Specification The primary responsibility of the StringLog ADT is to remember all the.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Java: Base Types All information has a type or class designation
Sixth Lecture ArrayList Abstract Class and Interface
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
Java: Base Types All information has a type or class designation
CompSci 230 S Programming Techniques
COP 3503 FALL 2012 Shayan Javed Lecture 8
Lecture 2: Implementing ArrayIntList reading:
CS 302 Week 8 Jim Williams, PhD.
Methods (toString, equals)
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Chapter 6 Arrays Fall 2012 CS2302: Programming Principles.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Grouped Data Arrays, and Array Lists.
Objects with ArrayLists as Attributes
Presentation transcript:

Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

Contents  Wrappers: Java’s Wrapper classes for the primitive types  Object class  Arrays of Object Fall 2012 CS2302: Programming Principles 2

Primitives & Wrappers  Java has a wrapper class for each of the eight primitive data types: Primitive Type Wrapper Class Primitive Type Wrapper Class booleanBooleanfloatFloat byteByteintInteger charCharacterlongLong doubleDoubleshortShort Fall 2012 CS2302: Programming Principles 3

Primitives vs. Wrappers int x = 25; Integer y = new Integer(33); Fall 2012 CS2302: Programming Principles 4

Use of the Wrapper Classes  Java’s primitive data types (boolean, int, etc.) are not classes.  Wrapper classes are used in situations where objects are required, such as for elements of a Collection: ArrayList a = new ArrayList (); a.add(new Integer(2)); Fall 2012 CS2302: Programming Principles 5

Value => Object: Wrapper Object Creation  Wrapper.valueOf() takes a value (or string) and returns an object of that class: Integer i1 = Integer.valueOf(42); Integer i2 = Integer.valueOf( “ 42 ” ); Boolean b1 = Boolean.valueOf(true); Boolean b2 = Boolean.valueOf( “ true ” ); Fall 2012 CS2302: Programming Principles 6

Object => Value  Each wrapper class Type has a method typeValue to obtain the object’s value: Integer i1 = Integer.valueOf(42); System.out.println(i1.intValue()); Boolean b1 = Boolean.valueOf( “ false ” ); System.out.println(b1.booleanValue()); => 42 false Fall 2012 CS2302: Programming Principles 7

The Object Class and Its Methods  java.lang.Object class  The toString() method returns a string representation of the object. The default implementation returns a string consisting of a class name of which the object is an instance, the at sign and a number representing this object. Circle c1 = new Circle(); System.out.println(c1.toString()); Fall 2012 CS2302: Programming Principles 8

Array of Object  The elements of an array can be object references  Declaration: – Object [ ] arrayRefVar ; Fall 2012 CS2302: Programming Principles 9

Basic Operations  Creation: – arrayRefVar = new Object[arraySize];  Declaring and Creating in one step: – Object[ ] myList = new Object[10]; Fall 2012 CS2302: Programming Principles 10

Class definition public class PartialArrayOfObject { /*Where to store the data.*/ private Object[] data; /*How many are actually stored.*/ private int numStored; /* Constructor: Initialize the array to the given size. This will be the maximum number that can be held.*/ public PartialArrayOfObject(int size) { data = new Object[size]; numStored = 0; } /*Get the element at index i.*/ public Object get(int i) { if(0 <= i && i < numStored) { return data[i]; } else { System.out.println(“Index is out of range.”); } /*Add an element to the end of the array.*/ public void add(Object val) { ………… } } Fall 2012 CS2302: Programming Principles 11

Class definition /*Add an element to the end of the array.*/ public void add(Object val) { if(numStored < data.length) { data[numStored] = val; numStored++; } else {// no more room System.out.println("Partial array is full"); } /*Insert the string val into the array so that it ends up with the given*/ public void insertAt(Object val, int index) { if(index numStored) { System.out.println(“Insert index out of bounds”); } else if(numStored >= data.length) {// no more room System.out.println("Partial array is full"); }else { for(int j = numStored; j > index; j--) { data[j] = data[j-1]; } data[index] = val; // put the new value in place numStored++; // one more element stored } Fall 2012 CS2302: Programming Principles 12

Create an integer array Fall 2012 CS2302: Programming Principles 13 public class Test { public static void main(String[] args) { /*create an object array with size 5*/ PartialArrayOfObject intArr = new PartialArrayOfObject(5); /*Fill the array and display each element*/ for(int i=0; i<5;i++){ intArr.add(i); System.out.println(“Element " + i + “ is: ” + intArr.get(i)); } /*Calculate total*/ int total = 0; for(int i=0; i<5;i++){ total += (Integer)intArr.get(i); } System.out.println("Total is " + total); }