0% found this document useful (0 votes)
21 views7 pages

56 Tanay JAVA Exp8

Hhgff

Uploaded by

Deadly Ninja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

56 Tanay JAVA Exp8

Hhgff

Uploaded by

Deadly Ninja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment No.

8
String Operations And Vector Class

Name: Tanay More Date:14/10


Roll No. 56 Batch: G3

Aim: a. WAP to reverse a complete sentence using string buffer class.

b. WAP to use methods of vector class.

Theory:
StringBuffer is a peer class of String that provides much of the functionality of strings. String
represents fixed-length, immutable character sequences. In contrast, StringBuffer represents
growable and writeable character sequences. StringBuffer may have characters and substrings
inserted in the middle or appended to the end. StringBuffer will automatically grow to make
room for such additions and often has more characters preallocated than are actually needed, to
allow room for growth. Java uses both classes heavily, but many programmers deal only with
String and let Java manipulate StringBuffers behind the scenes by using the overloaded +
operator.

StringBuffer defines these three constructors:


StringBuffer( )
StringBuffer(int size)
StringBuffer(String str)

Let's see the important methods of String

class. charAt( ) and set CharAt( )


The value of a single character can be obtained from a StringBuffer via the charAt( ) method.
You can set the value of a character within a StringBuffer using setCharAt( ). Their general
forms are shown here:
char charAt(int where)
void setCharAt(int where, char ch)

getChars( )
To copy a substring of a StringBuffer into an array, use the getChars( ) method. It has
this general form:
void getChars(int sourceStart, int sourceEnd, char target[
], int targetStart)

append( )
The append( ) method concatenates the string representation of any other type of
data to the end of the invoking StringBuffer object.

StringBuffer append(String str)


StringBuffer append(int num)
StringBuffer append(Object obj)
String.valueOf( ) is called for each parameter to obtain its string representation.The result is
appended to the current StringBuffer object. The buffer itself is returned by each version of
append( ).
insert( )
The insert( ) method inserts one string into another.

StringBuffer insert(int index, String str)


StringBuffer insert(int index, char ch)
StringBuffer insert(int index, Object obj)

Here, index specifies the index at which point the string will be inserted into the invoking
StringBuffer object.

reverse( )

You can reverse the characters within a StringBuffer object using reverse( ), shown here:
StringBuffer reverse( )

This method returns the reversed object on which it was called.

delete( ) and deleteCharAt( )

Java 2 added to StringBuffer the ability to delete characters using the methods delete( ) and
deleteCharAt( ). These methods are shown here:
StringBuffer delete(int startIndex, int endIndex)
StringBuffer deleteCharAt(int loc)

The delete( ) method deletes a sequence of characters from the invoking object. Here, startIndex
specifies the index of the first character to remove, and endIndex specifies an index one past the
last character to remove. Thus, the substring deleted runs from startIndex to endIndex–1. The
resulting StringBuffer object is returned.

replace( )

It replaces one set of characters with another set inside a StringBuffer object. Its signature is
shown here:
StringBuffer replace(int startIndex, int endIndex, String str)

The substring being replaced is specified by the indexes startIndex and endIndex. Thus, the
substring at startIndex through endIndex–1 is replaced. The replacement string is passed in str.
The resulting StringBuffer object is returned.
PROGRAM/CODE: a

OUTPUT a

Program Code : b

import java.util.*;
class VectorDemo
{
public static void main(String args[ ])
{

SIES GRADUATE SCHOOL OF Page 3


TECHNOLOGY
// initial size is 4, increment is 2
Vector v = new Vector(4,2);
System.out.println("Initial size : "+v.size());
System.out.println("Initial Capacity : "+v.capacity());
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
v.addElement(new Integer(5));
System.out.println("Capacity after five additions : "+v.capacity());
v.addElement(new Double(5.25));
System.out.println("Current Capacity : "+v.capacity());
v.addElement(new Double(10.28));
v.addElement(new Integer(8));
System.out.println("Current Capacity : "+v.capacity());
v.addElement(new Float(20.25));
v.addElement(new Integer(10));
System.out.println("Current Capacity : "+v.capacity());
v.addElement(new Integer(11));
v.addElement(new Integer(12));
System.out.println("\nFirst element :"+(Integer)v.firstElement());
System.out.println("Last element :"+(Integer)v.lastElement());
if(v.contains(new Double(5.20)))
System.out.println("Vector contains 5.20");
//****** Enumerate the elements in the Vector *********
Enumeration vEnum = v.elements();
System.out.println("\n Elements in vector :");

while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() +" ");
System.out.println();

System.out.println("index of: 11 "+v.indexOf(11));


System.out.println("element at 5:"+v.elementAt(5));
v.insertElementAt(new Integer(100), 5);
Enumeration vEnum1 = v.elements();
System.out.println("\n Elements in vector :");

while(vEnum1.hasMoreElements())
System.out.print(vEnum1.nextElement() +" ");
System.out.println();
}
}

OUTPUT b

SIES GRADUATE SCHOOL OF Page 4


TECHNOLOGY
Conclusion:
We reversed a String using Buffered reader function and used Vector class methods.

SIES GRADUATE SCHOOL OF Page 5


TECHNOLOGY
SIES GRADUATE SCHOOL OF Page 6
TECHNOLOGY
SIES GRADUATE SCHOOL OF Page 7
TECHNOLOGY

You might also like