Tutorial #10
In this tutorial we will try to understand the basic concept on Arrays, Strings & vectors that forms the backbone of the future topics that we are going to learn, as well as in the practical field of application development or web-development.
** Arrays
An array is a group of contiguous or related data items that share a common name , mainly they contains
element of same data-type, but inbuilt array-list in java also supports insertion of objects that may
deoesn't belong to the same class .
Types of arrays :-
1.One dimensional array (1D)
It represent basically one row or one column of elements .
e.g=> marks obtained by four students in a subject 50,51,52,53
2.Two dimensional array (2D)
It represent a two dimensional matrix that contains m rows & n columns.
e.g=> [a,b,c ; 1, 2, 3]
3.N-dimensional array (ND)
It can be visualized as an N-dimensional matrix in N-dimensional vector space that is not realizable or
being seen after N=3.
e.g=> [{a,b,c; 1,2,3 ; 4,5,6}; {a,b,c; 1,2,3 ; 4,5,6}; {a,b,c; 1,2,3 ; 4,5,6};{a,b,c; 1,2,3 ; 4,5,6}].
Creating an array:-
Creation of an array involves three steps:-
1. Declaring an array -
e.g=> [a,b,c ; 1, 2, 3]
3.N-dimensional array (ND)
It can be visualized as an N-dimensional matrix in N-dimensional vector space that is not realizable or
being seen after N=3.
e.g=> [{a,b,c; 1,2,3 ; 4,5,6}; {a,b,c; 1,2,3 ; 4,5,6}; {a,b,c; 1,2,3 ; 4,5,6};{a,b,c; 1,2,3 ; 4,5,6}].
Creating an array:-
Creation of an array involves three steps:-
1. Declaring an array -
type array_name[ ]; or,
type[ ] array_name;
Remember we do not enter the size of the array during declaration.
2. Creating an array
array_name = new type[size] ;
3.putting values into the memory location
array_name[subscript] = value;
e.g=> int table [2] [3] = {1,2,3,4,5,6} ;
Unlike in C, Java protects array from overruns & underruns. Trying to access an array bound its
boundaries will generate an error message.
** Array length
Java have an inbuilt function for getting the information about the number elements present in an array.
We can obtain the length of array "a" using a.length
e.g=> int aSize = a.length
** Strings
String simply represents the sequence of characters & Java have inbuilt class for strings manipulation & processing called as "String" class.
The syntax of initializing string is given below:-
String string_name ;
String_name = new String ("string") ;
e.g=> String firstName;
firstName = new String("Anil") ;
For more information on inbuilt String type & its per-implemented method visit the official site of Java given below
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
** Vectors
In this previous tutorial we saw the concept of variable argument method in Java. This feature can also be implemented in Java through the use of "Vector" class contained in "java.util" package.
This class can be used to create generic dynamic array known as vector that can hold objects of any type & any number. The objects do not have to homogeneous.
Following are the main advantage of vector over array:-
** Wrapper class
Primitive data-types (like char, int, float) may be converted into object type using wrapper classes
contained in java.lang package.
e.g=>
Primitive datatype-->Wrapper Class-->Constructor arguments
Method calling Conversion action
_______________________________________________________________________
** Take an example program that simply implementted Calender class of java
Unlike in C, Java protects array from overruns & underruns. Trying to access an array bound its
boundaries will generate an error message.
** Array length
Java have an inbuilt function for getting the information about the number elements present in an array.
We can obtain the length of array "a" using a.length
e.g=> int aSize = a.length
** Strings
String simply represents the sequence of characters & Java have inbuilt class for strings manipulation & processing called as "String" class.
The syntax of initializing string is given below:-
String string_name ;
String_name = new String ("string") ;
e.g=> String firstName;
firstName = new String("Anil") ;
For more information on inbuilt String type & its per-implemented method visit the official site of Java given below
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
** Vectors
In this previous tutorial we saw the concept of variable argument method in Java. This feature can also be implemented in Java through the use of "Vector" class contained in "java.util" package.
This class can be used to create generic dynamic array known as vector that can hold objects of any type & any number. The objects do not have to homogeneous.
Following are the main advantage of vector over array:-
- A vector can be used to store even inhomogeneous object that can very in size.
- We can add or delete objects from the list as & when required.
** Wrapper class
Primitive data-types (like char, int, float) may be converted into object type using wrapper classes
contained in java.lang package.
e.g=>
Primitive datatype-->Wrapper Class-->Constructor arguments
- boolean--> Boolean--> boolean or String
- byte--> Byte--> byte or String
- char--> Character--> char
- short--> Short--> short or String
- int-->Integer--> int or String
- long--> Long--> long or String
- float-->Float--> float double or String
- double-->Double--> double or String
Method calling Conversion action
- int i = IntVal.intValue( ); object to primitive integer
- float f = FloatVal.floatvalue( ); object to primitive float
- long l = LongValue.longValue( ); object to primitive long
- double d = DoubleVal.doubleValue( ); object to primitive double
_______________________________________________________________________
** Take an example program that simply implementted Calender class of java
import java.util.Calendar; import java.util.Locale; public class TestFormat { public static void main(String[] args) { long n = 461012; System.out.format("%d%n", n); // --> "461012" System.out.format("%08d%n", n); // --> "00461012" System.out.format("%+8d%n", n); // --> " +461012" System.out.format("%,8d%n", n); // --> " 461,012" System.out.format("%+,8d%n%n", n); // --> "+461,012" double pi = Math.PI; System.out.format("%f%n", pi); // --> "3.141593" System.out.format("%.3f%n", pi); // --> "3.142" System.out.format("%10.3f%n", pi); // --> " 3.142" System.out.format("%-10.3f%n", pi); // --> "3.142" System.out.format(Locale.FRANCE, "%-10.4f%n%n", pi); // --> "3,1416" Calendar c = Calendar.getInstance(); System.out.format("%tB %te, %tY%n", c, c, c); // --> "May 29, 2006" System.out.format("%tl:%tM %tp%n", c, c, c); // --> "2:34 am" System.out.format("%tD%n", c); // --> "05/29/06" } }______________________________________________________________________________
No comments:
Post a Comment