13
Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

Embed Size (px)

Citation preview

Page 1: Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

1

Arrays of Objects

Fall 2012 CS2302: Programming Principles

Page 2: 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 Principles2

Page 3: Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

Primitives & Wrappers

Java has a wrapper class for each of the eight primitive data types:

Primitive Type

Wrapper Class

Primitive Type

Wrapper Class

boolean Boolean float Float

byte Byte int Integer

char Character long Long

double Double short Short

Fall 2012 CS2302: Programming Principles3

Page 4: Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

Primitives vs. Wrappers

int x = 25;Integer y = new Integer(33);

Fall 2012 CS2302: Programming Principles4

Page 5: Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

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<Integer> a = new ArrayList<Integer>(); a.add(new Integer(2));

Fall 2012 CS2302: Programming Principles5

Page 6: Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

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 Principles6

Page 7: Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

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 Principles7

Page 8: Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

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 Principles8

Page 9: Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

Array of Object

The elements of an array can be object references

Declaration:– Object [ ] arrayRefVar;

Fall 2012 CS2302: Programming Principles9

Page 10: Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

Basic Operations

Creation:– arrayRefVar = new Object[arraySize];

Declaring and Creating in one step:– Object[ ] myList = new Object[10];

Fall 2012 CS2302: Programming Principles10

Page 11: Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

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 Principles11

Page 12: Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

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 roomSystem.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 < 0 || index > numStored) {System.out.println(“Insert index out of bounds”);

} else if(numStored >= data.length) {// no more roomSystem.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 placenumStored++; // one more element stored}

}}

Fall 2012 CS2302: Programming Principles12

Page 13: Arrays of Objects 1 Fall 2012 CS2302: Programming Principles

Create an integer array

Fall 2012 CS2302: Programming Principles13

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); }}