Lec16 - Arrays Misc, Wrapper Classes and and ArrayList

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Lecture 16

16.1 Command Line Arguments


16.2 Variable Length Arguments
16.3 Utility methods of Arrays class
16.4 Wrapper Classes in Java
16.5 Auto Boxing and UnBoxing
16.6 Life without ArrayList
16.7 Using ArrayList

By: Asif Shahzad


Command Line Arguments
• Command line arguments are used to pass some data to a
program when a Java program runs. This data is called
Command Line Arguments.
import java.util.Scanner;

public class StudentTest{

public static void main(String args[]){


String name = args[0];
String email = args[1];
Student std = new Student(name, email);
}
}
• You can run it like this:
• C:>java StudentTest Ali [email protected]
Variable Length Arguments using …
• static void abc(int… numbers){
for(int n: numbers)
…print(n);
}
• How to call such methods:
abc();
abc(1);
abc(1,2,5);
abc(new int[]{7,8, 9});

• 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]);

• To convert an array to a list. Use Arrays class’s asList method.


ArrayList<String> list = Arrays.asList(new String[]{“Ali”, “Zia”});

• asList method takes variable length argument, so you can also


use it like this:
ArrayList<String> list = Arrays.asList("one", "two");

You might also like