Download presentation
Presentation is loading. Please wait.
Published byCollin Woods Modified over 9 years ago
1
Arrays of Objects 1 Fall 2012 CS2302: Programming Principles
2
Contents Wrappers: Java’s Wrapper classes for the primitive types Object class Arrays of Object Fall 2012 CS2302: Programming Principles 2
3
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
4
Primitives vs. Wrappers int x = 25; Integer y = new Integer(33); Fall 2012 CS2302: Programming Principles 4
5
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
6
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
7
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
8
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
9
Array of Object The elements of an array can be object references Declaration: – Object [ ] arrayRefVar ; Fall 2012 CS2302: Programming Principles 9
10
Basic Operations Creation: – arrayRefVar = new Object[arraySize]; Declaring and Creating in one step: – Object[ ] myList = new Object[10]; Fall 2012 CS2302: Programming Principles 10
11
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
12
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
13
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); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.