Transcript Document
Arrays of Objects
1
Fall 2012 CS2302: Programming Principles
Contents
2
Wrappers: Java’s Wrapper classes
for the primitive types
Object class
Arrays of Object
Fall 2012 CS2302: Programming Principles
Primitives & Wrappers
3
Java has a wrapper class for each of the
eight primitive data types:
Primitive
Type
Wrapper
Class
Primitive
Type
Wrapper
Class
boolean
byte
char
Boolean
Byte
Character
float
int
long
Float
Integer
Long
double
Double
short
Short
Fall 2012 CS2302: Programming Principles
Primitives vs. Wrappers
int x = 25;
Integer y = new Integer(33);
4
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));
5
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”);
6
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
7
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());
8
Fall 2012 CS2302: Programming Principles
Array of Object
The elements of an array can be object
references
Declaration:
–
9
Object [ ] arrayRefVar;
Fall 2012 CS2302: Programming Principles
Basic Operations
Creation:
–
Declaring and Creating in one step:
–
10
arrayRefVar = new Object[arraySize];
Object[ ] myList = new Object[10];
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) { ………… }
}
11
Fall 2012 CS2302: Programming Principles
Class definition
12
/*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 < 0 || 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
Create an integer array
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);
}
}
13
Fall 2012 CS2302: Programming Principles