Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays of Objects Fall 2012 CS2302: Programming Principles.

Similar presentations


Presentation on theme: "Arrays of Objects Fall 2012 CS2302: Programming Principles."— Presentation transcript:

1 Arrays of Objects Fall CS2302: Programming Principles

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

3 Java has a wrapper class for each of the eight primitive data types:
Primitives & Wrappers Java has a wrapper class for each of the eight primitive data types: Primitive Type Wrapper Class boolean Boolean float Float byte Byte int Integer char Character long Long double Double short Short Fall CS2302: Programming Principles

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

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<Integer> a = new ArrayList<Integer>(); a.add(new Integer(2)); Fall CS2302: Programming Principles

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 CS2302: Programming Principles

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 CS2302: Programming Principles

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 CS2302: Programming Principles

9 The elements of an array can be object references
Array of Object The elements of an array can be object references Declaration: ArrayList<Object> arrayRefVar; Fall CS2302: Programming Principles

10 Declaring and Creating in one step:
Basic Operations Creation: arrayRefVar = new ArrayList<Object>(); Declaring and Creating in one step: ArrayList<Object> myList = new ArrayList<Object>(); Fall CS2302: Programming Principles

11 Class definition public class PartialArrayOfObject { /*Where to store the data.*/ private ArrayList<Object> data; /* Constructor: Initialize the array to the given size. This will be the maximum number that can be held.*/ public PartialArrayOfObject(int size) { data = new ArrayList<Object>(); numStored = 0; } /*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.add(index,val); // put the new value in place Fall CS2302: Programming Principles

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


Download ppt "Arrays of Objects Fall 2012 CS2302: Programming Principles."

Similar presentations


Ads by Google