13.ARRAYS
14.WRAPPER CLASSES
15.STRINGS
16.STRING BUFFER/BUILDER
17.STRING TOKENIZER
ARRAYS:
******* Arrays has a some difference from c and cpp arrays.we have to create memory
and size of the array.Array is a group of similar data items that share
a common name.A particular value is indicated by writing a number called
index or subscript. ex: num[10]
1.single dimensional
2.multi dimensional
one dimensional array:
A list of items can be given one variable name using only one subscript
and such a variable is called a single subscripted variable or a one dimensional
array.
it is different from c.why because in c we should give the array size at the
time of declaration.and it does not require the special memory creation but
in java we have to declare the array without size and we have to create memoyr
for that array.
ex: int no[]; int[] no;
no[]=new int[5];//creation of array and memory also
int no[]={34,34,34,34};//initilization
'length' is a predifined variabe it used for knowig the lengtn of the array.
int a=no.length;
2two dimensional array:
A list of items can be given one variable name using two subscript
and such a variable is called a multi subscripted variable or a multi dimensional
array.
int no[][]={ {34,354,45},{34,34,83},{34,88,93} }//intialization
int no[0][0]=34;
int no[1][1]=34;
int no[2][2]=88;
int no[][]=new int[3][3];// creation of memory and declaration;
WRAPER CLASSES:
***************
In 'c' we have 'atoi','atof','atol','atod' to convert the types.wrapper is
nothing but a 'new look'.sometimes you need to convert the primitive type to an
object and string formatted to number values into it's primitives type and vice versa.
so for this application java provides wrapper classes.
All primitive types have their classes means an 'int' type has it's Integer class
and 'float' type has 'Float' class and soon. These kind of classes are called wrapper
.wrapper classes provides by java.Each primitives has it's own type wrapper class.
These are Character,Byte,Short,Integer,Long,Float Double,Boolean.
*These classes are final and static so they cannot be inherited.*
Byte:
--------
//constants
static final byte MAX-VALUE
static final byte MIN-VALUE
//cnstructors
Byte(byte value)
Byte( String s)
Integer:
--------
//constants
static final int MAX-VALUE
static final int MIN-VALUE
//CONSTRUCTORS
Integer(int value)
Integer(String str)
//class methods
static int parseInt(String str)
note:the above constants and parser methods are avaliable in every wrapper class.
//instance methods
byte byteValue()
short shortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()
String toString()
STRINGS:
********
This is a class in java.lang package. The String class represents character
Arrays.All string literals in java programs,such as "abc" are implemented
as instances of this class.
Strings are constants,thier values cannot be changed after they are created.
String buffers support mutable strings. Because String objects are immutable
they can be shared.
for example:
String str="abc";
is equalent to : char data[]={'a','b','c'};
string str=new String(data);
The class string includes methods for examining individual
characters of the sequence,for comparing strings,for extracting sub strings,
and for creating a copy of a string with all characters translated
to uppercase or to lowercase.
The java provides special for the string concatnation operator(+)
,and for conversion of objects to strings.String concatenation
is implemented througe the StringBuffer class and its append
method.
---------------
methods:
public int length()// to get the length of string
public char charAt(index)//to get the character
public boolean equals(Object anObject)
public equalsIgnoreCase(String anotherstring)
public int compareTo(String anotherstring)
public boolean startsWith(String prefix)
public boolean endsWith(String suffix)
public indexOf(int ch)
public int lastIndexOf(int ch)// check
public int indexOf(String str)
public lastIndexOf(String str)
public String subString(int beginindex)
public String concat(String str)
public String replace(char oldchar,char newchar)
public String toLowerCase()
public String toUpperCase()
public String trim()
----------------
these are methods provided by java to perform operation on strings
but one thing we have to remember i.e., after performing operations
it returns new string.it does not change the originol string.it wastes the memory
for new strings.
STRING BUFFER/BUILDER:
**********************
String buffer is as same as String class but it has more functionality and
more flexibility available in java.lang package. It can be extended with some memory
every time.when user try to store more strings content into object.
constructors:
StringBuffer()//default with size 16 bytes
StringBuffer(int size)// size can be mentioned
StrngBuffer(String str)//with string object
methods:
int length()//string length
int capacity()// capacity of buffer
char charAt(int where)// character of given position
void setCharAt(int where,char ch)//replace old char with new
StringBuffer append(String str)//to add more content
StringBuffer insert(int where,String str)//t insert String in given pos
StringBuffer reverse()//to reverse
StringBuffer delete(int startpos,int endposi)//to delete text
StringBuffer deleteCharAt(int location)//to remove character
StringBuffer replace(int startpos,int endpos,String str)
String subString(int substring)
String subString(int startpos,int endposi)
PROGRAM:
import java.lang.*;
class stringBufer
{
public static void main(String args[])
{
String str="one two three four five";
StringBuffer sb=new StringBuffer(str);
System.out.println("\n\n\n\t"+sb.capacity()+"\n\n\n\t"+sb.length()+sb);
}
}
STRINGTOKENIZER:
****************
StringTokenizer is a particular class availabel in java.util package.
to divide a long length string into words are called 'tokens'.for this dividing
the input string should have the delimiters like characters,generally ' '(space) or,
newline(\n) or carrigeReturnwords(\r) etc.......
default delimiter is space.
class: java.util.StringTokenizer
constructors:
StringTokenizer(String str)// default delimiter is taken
SrringTokenizer(String str,String delimiter)//can specify delimiter
SrringTokenizer(String str,String delimiter,boolean value)//value
denotes it consider delimiter as token
methods:
int countTokens()//number of tokens
boolean hasMoreTokens()//return true if more tokens exits
Object nextElement()//return next token as objefct
String nextElement()//return next token as string
String nextToken(String delimiters//to return next token and can
set new dekimiters
program:
import java.lang.*;
import java.util.*;
class StringTokenizerTest
{
public static void main(String args[])
{
String line="one two three four five";
line+="six seven eight nine ten";
StringTokenizer stk=new StringTokenizer(line);
//StringTokenizer stk=new StringTokenizer(line,",");
//StringTokenizer stk=new StringTokenizer(line,",",true);
while(stk.hasMoreTokens())
{
System.out.println("\n\ttotaltokens are "+stk.countTokens());
System.out.println("\t"+stk.nextToken());
}
}
}
No comments:
Post a Comment