Java Arrays and Date&Time
Functions
•Declaring and Initializing arrays.
•Date and Time Functions
4/10/2019 1Jamsher Bhanbhro(F16CS11)
Array
• An array is the collection of the same data type.
• Array is used for minimization of code.
• Array must be declared and initialized before using in
the program.
• Array can be declared by using a reference variable that
will refer the array.
• Data type[] refvar; this is syntax for declaring an array.
Eg int[] marks;
• Array can be initialized or declared as: int[]
marks={values}, or int[] marks=new int[values].
4/10/2019 Jamsher Bhanbhro(F16CS11) 2
Arrays Class
• Import java.arrays.*; imports many static
methods for searching and sorting.
• public static int binarySearch(Obj[]a, obj
key);
• public static boolean equals(long[] a, long[]
a2) ;
• public static void fill(int[] a, int val) ;
• public static void sort(Object[] a);
4/10/2019 Jamsher Bhanbhro(F16CS11) 3
Methods…
• public static int binarySearch(Object[] a,
Object key)
• Searches the specified array of Object ( Byte, Int ,
double, etc.) for the specified value using the
binary search algorithm. The array must be sorted
prior to making this call. This returns index of the
search key, if it is contained in the list; otherwise,
it returns ( – (insertion point + 1)).
4/10/2019 Jamsher Bhanbhro(F16CS11) 4
Cont…
• public static boolean equals(long[] a, long[] a2)
• Returns true if the two specified arrays of longs
are equal to one another. Two arrays are
considered equal if both arrays contain the same
number of elements, and all corresponding pairs
of elements in the two arrays are equal. This
returns true if the two arrays are equal. Same
method could be used by all other primitive data
types (Byte, short, Int, etc.)
4/10/2019 Jamsher Bhanbhro(F16CS11) 5
Cont…
• public static void fill(int[] a, int val):
• Assigns the specified int value to each element
of the specified array of ints. The same method
could be used by all other primitive data types
(Byte, short, Int, etc.)
4/10/2019 Jamsher Bhanbhro(F16CS11) 6
Cont…
• public static void sort(Object[] a):
• Sorts the specified array of objects into an
ascending order, according to the natural
ordering of its elements. The same method
could be used by all other primitive data types
( Byte, short, Int, etc.)
4/10/2019 Jamsher Bhanbhro(F16CS11) 7
Date&Time
• Java has a class Date and it is imported in
java.util.*; library.
• Date class has two constructors.
• Date( ) : This constructor initializes the object
with the current date and time.
• Date(long millisec): This constructor accepts
an argument that equals the number of
milliseconds that have elapsed since midnight,
January 1, 1970.
4/10/2019 Jamsher Bhanbhro(F16CS11) 8
Date class Methods
• boolean after(Date date)/before(Date date): this method returns
after/before date comparison with initialized date returns false if
condition becomes false.
• Object clone( ): Duplicates the invoking Date object.
• int compareTo(Date date): Compares the value of the invoking
object with that of date. Returns 0 if the values are equal. Returns a
negative value if the invoking object is earlier than date. Returns a
positive value if the invoking object is later than date.
• int compareTo(Object obj) :Operates identically to
compareTo(Date) if obj is of class Date. Otherwise, it throws a
ClassCastException.
• boolean equals(Object date) :Returns true if the invoking Date
object contains the same time and date as the one specified by
date, otherwise, it returns false.
4/10/2019 Jamsher Bhanbhro(F16CS11) 9
Cont…
• long getTime( ): Returns the number of
milliseconds that have elapsed since January 1,
1970.
• int hashCode( ): Returns a hash code for the
invoking object.
• void setTime(long time): Sets the time and date
as specified by time, which represents an elapsed
time in milliseconds from midnight, January 1,
1970
• String toString( ): Converts the invoking Date
object into a string and returns the result.
4/10/2019 Jamsher Bhanbhro(F16CS11) 10
Tasks
• Write two programs to implement array
functions.
• Write a program and use all Date class
functions and constructors.
4/10/2019 Jamsher Bhanbhro(F16CS11) 11

Java Arrays and DateTime Functions

  • 1.
    Java Arrays andDate&Time Functions •Declaring and Initializing arrays. •Date and Time Functions 4/10/2019 1Jamsher Bhanbhro(F16CS11)
  • 2.
    Array • An arrayis the collection of the same data type. • Array is used for minimization of code. • Array must be declared and initialized before using in the program. • Array can be declared by using a reference variable that will refer the array. • Data type[] refvar; this is syntax for declaring an array. Eg int[] marks; • Array can be initialized or declared as: int[] marks={values}, or int[] marks=new int[values]. 4/10/2019 Jamsher Bhanbhro(F16CS11) 2
  • 3.
    Arrays Class • Importjava.arrays.*; imports many static methods for searching and sorting. • public static int binarySearch(Obj[]a, obj key); • public static boolean equals(long[] a, long[] a2) ; • public static void fill(int[] a, int val) ; • public static void sort(Object[] a); 4/10/2019 Jamsher Bhanbhro(F16CS11) 3
  • 4.
    Methods… • public staticint binarySearch(Object[] a, Object key) • Searches the specified array of Object ( Byte, Int , double, etc.) for the specified value using the binary search algorithm. The array must be sorted prior to making this call. This returns index of the search key, if it is contained in the list; otherwise, it returns ( – (insertion point + 1)). 4/10/2019 Jamsher Bhanbhro(F16CS11) 4
  • 5.
    Cont… • public staticboolean equals(long[] a, long[] a2) • Returns true if the two specified arrays of longs are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. This returns true if the two arrays are equal. Same method could be used by all other primitive data types (Byte, short, Int, etc.) 4/10/2019 Jamsher Bhanbhro(F16CS11) 5
  • 6.
    Cont… • public staticvoid fill(int[] a, int val): • Assigns the specified int value to each element of the specified array of ints. The same method could be used by all other primitive data types (Byte, short, Int, etc.) 4/10/2019 Jamsher Bhanbhro(F16CS11) 6
  • 7.
    Cont… • public staticvoid sort(Object[] a): • Sorts the specified array of objects into an ascending order, according to the natural ordering of its elements. The same method could be used by all other primitive data types ( Byte, short, Int, etc.) 4/10/2019 Jamsher Bhanbhro(F16CS11) 7
  • 8.
    Date&Time • Java hasa class Date and it is imported in java.util.*; library. • Date class has two constructors. • Date( ) : This constructor initializes the object with the current date and time. • Date(long millisec): This constructor accepts an argument that equals the number of milliseconds that have elapsed since midnight, January 1, 1970. 4/10/2019 Jamsher Bhanbhro(F16CS11) 8
  • 9.
    Date class Methods •boolean after(Date date)/before(Date date): this method returns after/before date comparison with initialized date returns false if condition becomes false. • Object clone( ): Duplicates the invoking Date object. • int compareTo(Date date): Compares the value of the invoking object with that of date. Returns 0 if the values are equal. Returns a negative value if the invoking object is earlier than date. Returns a positive value if the invoking object is later than date. • int compareTo(Object obj) :Operates identically to compareTo(Date) if obj is of class Date. Otherwise, it throws a ClassCastException. • boolean equals(Object date) :Returns true if the invoking Date object contains the same time and date as the one specified by date, otherwise, it returns false. 4/10/2019 Jamsher Bhanbhro(F16CS11) 9
  • 10.
    Cont… • long getTime(): Returns the number of milliseconds that have elapsed since January 1, 1970. • int hashCode( ): Returns a hash code for the invoking object. • void setTime(long time): Sets the time and date as specified by time, which represents an elapsed time in milliseconds from midnight, January 1, 1970 • String toString( ): Converts the invoking Date object into a string and returns the result. 4/10/2019 Jamsher Bhanbhro(F16CS11) 10
  • 11.
    Tasks • Write twoprograms to implement array functions. • Write a program and use all Date class functions and constructors. 4/10/2019 Jamsher Bhanbhro(F16CS11) 11