session 5 java.lang package using array java.io package: stringtokenizer, arraylist, vector using...

30
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Upload: nora-daniel

Post on 26-Dec-2015

252 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Session 5

java.lang packageUsing arrayjava.io package: StringTokenizer, ArrayList, VectorUsing Generic

Page 2: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

java.lang package The package consists classes and interface which are

fundamental to Java programming All the program automaticlly import java.lang package It contains

Page 3: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Wrapper classes

Page 4: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Wrapper classes (cont…)

Page 5: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Common methods

Page 6: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Math class Defining methods for basic numeric

operations as well as trigonomethic functions

All the methods are static This is final class Methods

public static double pow(double x, double y) public static double exp(double x) public static double log(double x)

Page 7: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Methods of Math class Trigonometric methods

public static double sin(double x) public static double cos(double x) public static double tan(double x)

Arirthmetic methods

Page 8: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Array Stored many values Elements in array have the same datatype Elements in array are accessed throught by

subscript

Types of array: Single dimension array (one dimension array) Multi demension array

Page 9: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

One Dimensional Array Declarations Three ways to declare an array are:

datatype identifier [ ];

int a[]; or int [] a;

datatype identifier [ ] = new datatype[size];

char ch[] = new char[10];

datatype identifier [ ] = {value1,value2,….valueN};

byte b[] = {12, 3, 5};

Page 10: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Initialize elements in array Init value for each elementa[0] = 12;

a[1] = 15; Init when creating arrayint a[] = {12, 15, 17};

Using loopfor(int i = 0 ; i < a.length ; i++)

a[i] = …;

Page 11: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Example

class ArrDemo{public static void main(String [] arg){double nums[] = {10.1, 11.3, 12.5,13.7, 14.9};System.out.println(" The value at location 3 is : " +

nums[3]);}

}

Page 12: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Two dimension array Array that has more than one dimention

Syntax:int a[][];

int []a[] = new int[2][3];

int [][]a = {{1, 2}, {3, 4}, {-9, 0}};

a[0][0] a[0][1] a[0][2]

a[0] 1 3 5

a[1] 9 -81 7

a[1][0] a[1][1] a[1][2]

Page 13: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Initialize 2 dimension array Initialize for each elementsa[0][0] = ‘c’;

a[0][1] = ‘e’; Initialize when declaring arrayint a[][] = {{1, 2}, {13, -78}};

Using loopfor(int i = 0 ; i < n ; i++)

for(int j = 0 ; j < m ; j++)

a[i][j] = …;

Page 14: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

String class In Java, a string literal is an object of

type String class. Hence manipulation of strings will be

done through the use of the methods provided by the String class.

String class is in java.lang package Every time we need an altered version

of the String, a new String object is created with the modifications in it.

Page 15: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Compare two String String length(): This method determines the

length of a string. The == operator and equals(), compareTo()

method can be used for strings comparison. The == operator checks if the two operands being used

are one and the same object. The equals() method checks if the contents of the two

operands are the same. compareTo() return value = 0 if s1 = s2, return value >0

if s1 > s2, return value < 0 if s1 < s2

Page 16: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Methods of String trim () substring (int start, int

end) equals (Object s) equalsIgnoreCase (String

s)

charAt(int i)

endsWith (String s)

startsWith (String s)

indexOf (String s)

lastIndexOf (String s)

toLowerCase ()

toUpperCase ()

Page 17: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Exampleclass StringTest {

public static void main(String[] args) { String name = args[0]; if(name.startsWith("M")) System.out.println("Hey my name also starts with an M! "); int length = name.length(); System.out.println("Your name has "+length+" characters"); String name_in_caps = name.toUpperCase(); System.out.println(name_in_caps); }}

Page 18: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Convert String into numberString s = “12”;

String to intint a = Interger.parseInt(s);

String to floatfloat f = Float.parseFloat(s);

Page 19: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Array of Strings

String s[];

s = new String[3];

Or String s[] = new String[3];

Or String []s = {“a”, “b”, “c”};

Learn Java By Example 19/21

Page 20: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Immutability of String Strings in Java once created cannot be

changed directly.

This is known as immutability in Strings.

Learn Java By Example 20/21

Page 21: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Exampleclass Testing { public static void main(String[] args) {

String str = "Hello"; str.concat("And Goodbye"); System.out.println(str); }}

Learn Java By Example 21/21

Page 22: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

java.util package Contains useful classes

providing a broard range of functionality

Collection classes are useful for working with groups of objects

Contains classes that provides date and time, calendar, dictionary facilities

Page 23: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

StringTokenizer Placed at java.util Seperated String into token Methods

countTokens() hasMoreElements() hasMoreTokens() nextElement() nextToken()

Example: s = “22+3-5/3” Token: 22 3 5 3 Separator: + - /

Page 24: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

ArrayList An ArrayList object is a variable length

array of object references. Used to create dynamic arrays Extends AbstractList and implements List interface.

ArrayLists are created with an initial size.

As elements are added, size increases and the array expands.

Page 25: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

ArrayList (cont…) Constructors Methods

•size()

•add(int index, E element)

•get(int index)

•indexOf(Object elem)

•lastIndexOf(Object elem)

•remove(int index)

•remove(Object o)

•set(int index, E element)

Page 26: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Vector class Similar to ArrayList class, allows to implement

dynamic array Storing an array of objects that size can increase

or decrease At any given point of time, an instance of type Vector has the capacity to hold a certain number of elements.

When it becomes full, its capacity is incremented by an amount specific to that Vector object.

Diffirence between Vector and ArrayList is that methods of Vector are synchronized and are thread-safe

Page 27: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Vector class (cont…)

Page 28: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Generic Earlier Collection treated elements as a collection of objects.

To retrieve an element from a Collection required an explicit cast

Thus, there was always a risk of runtime exception, ClassCastException

Generic allows the programmer to communicate the type of a collection to the compiler so that it can be checked.

The compiler consistently checks for the element type of the collection.

And inserts the correct cast on elements being taken out of the collection.

Page 29: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Example

Page 30: Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

Example of Generic Code

required explicit cast

No required explicit cast

Type of element