Lecture 8 Wrapper Classes, Arrays
Lecture 8 Wrapper Classes, Arrays
CIC-212
Java Programming
Lecture
Java’s Wrapper Classes for the
Primitives Types, Arrays and
Collections
Primitives & Wrappers
Java has a wrapper class for each of the eight primitive data types.
Wrapper classes are available in java.lang package
UNBOXING: Wrapper object will be converted back to a primitive data type, and this process is
called unboxing. The Number class is part of the java.lang package.
Integer i1 = Integer.valueOf(42);
Integer i2 = Integer.valueOf(“42”);
Long n1 = Long.valueOf(42000000L);
Long n1 = Long.valueOf(“42000000L”);
Accessing values from the Objects
Each wrapper class Type has a method typeValue to obtain the
object’s value:
int i = i1.intValue();
boolean b = b1.booleanValue();
long l = l1.longValue();
42
false
42000000
Converting String to Primitive type
The Wrapper class for each primitive type has a method parseType() to
parse a string representation & return the literal value.
•This class contains various methods for manipulating arrays (such as sorting and
searching).
•The methods in this class throw a NullPointerException if the specified array reference is
null.
static int binarySearch(byte[] a, byte key) This method searches the specified array of bytes
for the specified value using the binary search algorithm.
static boolean equals(long[] a, long[] a2) This method returns true if the two specified
arrays of longs are equal to one another.
static int hashCode(byte[] a) This method returns a hash code based on the contents of the
specified array.
static void sort(char[] a) This method sorts the specified array of chars into ascending
numerical order.
Around 105 methods defined in Arrays class to do different operations on arrays.
Java Arrays – Sorting
Again no need to do this “by hand”.
The java.util.Arrays class has methods to sort different kinds
of arrays
Advantages
• Very flexible, can hold any kind of object
Disadvantages
• Not as efficient as arrays (for some uses)
• Not type-safe. Store references to Object
Java Collections
Two Types of Containers
• Collections
• Group of objects, which may restricted or
manipulated in some way
• E.g. an ordered to make a List or LinkedList
• E.g. a Set, an unordered group which can only contain
one of each item
• Maps
• Associative array, Dictionary, Lookup Table, Hash
• A group of name-value pairs
Java Collections
• Maps
• HashMap, SortedMap
• Lists
• ArrayList, LinkedList
• Sets
• HashSet, SortedSet, Treeset
• Dictionary
• Stack
Array and Arraylist in Java
Array: Simple fixed sized arrays that we create in Java, like
below
int arr[] = new int[10];
import java.util.ArrayList;
class Test
// not allowed (Uncommenting below line causes
{
compiler error)
public static void main(String args[])
// ArrayList<char> arrL = new ArrayList<char>();
{
// allowed
// Allowed
int[] array = new int[3];
ArrayList<Integer> arrL1 = new ArrayList<>();
ArrayList<String> arrL2 = new ArrayList<>();
// allowed, however, need to be intialized
ArrayList<Object> arrL3 = new ArrayList<>();
Test[] array1 = new Test[3];
}
}
Java Collections – The Basics
List myList = new ArrayList();
List otherList = new ArrayList(5);
Map database = new HashMap();
Set things = new HashSet(); Copying a list - Very easy, just use
addAll()
For Collections, use add() List myList = new ArrayList();
List myList = new ArrayList();
//assume we add items to the list
myList.add(“A String”);
myList.add(“Other String”);
List otherList = new ArrayList();
For Maps, use put() otherList.addAll(myList);
Map myMap = new HashMap();
myMap.put(“google”, “http://www.google.com”);
mpMap.put(“yahoo”, “http://www.yahoo.com”);
Collections – Getting Individual Items
Use get()
Note that we have to cast the object to its original
type.
• Collections…
String s = (String)myList.get(1); //get first element
String s2 = (String)myList.get(10); //get tenth element
• Maps…
String s = (String)myMap.get(“google”);
String s2 = (String)mpMap.get(“yahoo”);
Collections – Getting all items
For Lists, we could use a for loop, and loop through the list to
get() each item.