Lec16 - Arrays Misc, Wrapper Classes and and ArrayList
Lec16 - Arrays Misc, Wrapper Classes and and ArrayList
Lec16 - Arrays Misc, Wrapper Classes and and ArrayList
• If there are multiple arguments, the variable length argument should appear at end.
• You may skip the value of variable length argument, the size of array would be 0.
• There can be only one argument of variable length in a method.
Arrays class methods
• java.util.Arrays
• Some basic method to work with Arrays
• fill
• sort
• equals
• binarySearch
• copyOf
Wrapper Classes
• primitive types do not belong to any
class; they are defined in language core.
Sometimes, it is required to convert
primitive type into object.
• For each primitive type, there is a class,
known as wrapper class, because it
"wrap" the corresponding primitive
type.
• Wrapper classes gives primitive an
object appearance. Wherever, the
primitive data is required as an object,
this wrapper class object can be used.
Useful Methods of Wrapper Classes
• Some useful methods and Attributes
• Example from Integer Class
• int parseInt(String n)
• Integer valueOf(int n)
• Integer valueOf(String n)
• MAX_VALUE
• MIN_VALUE
Auto Boxing and UnBoxing
• When you pass an int variable where Integer object is
expected, its auto converted to Integer object, called
Auto Boxing
• When Integer object is used where int variable is
expected, its converted to int value, called UnBoxing
• Integer n = 3;
int m = new Integer(10);
if(n > m)
System.out.println(“greater”);
ArrayList
• A collection that hold multiple objects. Its capacity is managed
internally, so we can add/remove elements as per requirement
without worrying creating new object, as we do in case of arrays.
• Internally, it uses array to store elements. ArrayList instance has
a capacity attribute, its the size of the array used to store the
elements in the list.
• As you add elements and array is filled, capacity grows
automatically. Initial capacity is 10.
• You may ensure capacity using:
• public void ensureCapacity(int minCapacity)
ArrayList
• You must know how to use following methods:
• boolean add(E e)
• void add(int index, E element)
• boolean remove(Object o)
• E remove(int index)
• E set(int index, E newElement)
• boolean isEmpty()
• int size()
• void clear()
• boolean contains(Object o)
• boolean equals(Object o)
Conversion between Array and ArrayList
• Use toArray method to convert a list to an array. It takes an
array of 0 size of same type that list hold. E.g.
String[] stringArray = list.toArray(new String[0]);