• No results found

09_String_Arrays_Vectors.pdf

N/A
N/A
Protected

Academic year: 2020

Share "09_String_Arrays_Vectors.pdf"

Copied!
25
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Class String

java.lang.Object

java.lang.String

public final class String extends Object

•The String class represents character strings

(3)

How to Create String objects

String str = "abc";

char data[] = {'a', 'b', 'c'};

String str = new String(data);

String cde = "cde";

(4)

C lass String has method for

The class String includes methods for

• examining individual characters of the sequence

• comparing strings

• searching strings

• extracting substrings

• creating a copy of a string

(5)
(6)

char charAt(int index) Returns the char value at the specified index.

int

compareTo(String anotherString) Compares two strings lexicographically.

int

compareToIgnoreCase(String str) Compares two strings lexicographically, ignoring case differences.

String

concat(String str) Concatenates the specified string to the end of this string.

boolean

contentEquals(StringBuffer sb) Compares this string to the specified StringBuffer.

static String

copyValueOf(char[] data) Returns a String that represents the character sequence in the array specified.

static String

copyValueOf(char[] data, int offset, int count) Returns a String that represents the character sequence in the array specified.

boolean

endsWith(String suffix) Tests if this string ends with the specified suffix.

(7)

boolean equalsIgnoreCase(String anotherString) Compares this String to another String, ignoring case considerations.

int hashCode() Returns a hash code for this string.

int indexOf(int ch) Returns the index within this string of the first occurrence of the specified character.

int indexOf(int ch, int fromIndex) Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

int indexOf(String str) Returns the index within this string of the first occurrence of the specified substring.

int indexOf(String str, int fromIndex) Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

boolean isEmpty() Returns true if, and only if, length() is 0.

int lastIndexOf(int ch) Returns the index within this string of the last occurrence of the specified character.

int lastIndexOf(int ch, int fromIndex) Returns the index within this string of the last occurrence of the specified character, searching backward

(8)

int

lastIndexOf(String str) Returns the index within this string of the rightmost occurrence of the specified substring.

int

lastIndexOf(String str, int fromIndex) Returns the index within this string of the last occurrence of the specified substring,

searching backward starting at the specified index. int length() Returns the length of this string.

char[] toCharArray() Converts this string to a new character array. String toLowerCase()

String

toString() This object (which is already a string!) is itself returned.

(9)

static String valueOf(boolean b) static String valueOf(char c)

static String valueOf(char[] data)

static String valueOf(char[] data, int offset, int count) static String valueOf(double d)

static String valueOf(float f) static String valueOf(int i) static String valueOf(long l)

static String

(10)

Substring

String Str=“abcdefgh”;

Str.subString(3);

Str.subString(2,4);

Str.subString(3,3)

cefgh

(11)

length or length()

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

a.length

2

(12)

indexOf()

String str=“Nihar ranjan roy”;

int i=str.indexOf(‘a’);

(13)

charAt()

String str=“Nihar”;

Char ch=str.charAt(2);

replace()replaces every occurrence of on character with another.

String lang=“english”;

(14)

equals() vs ==

“nihar”.equals(“nihar”); Or

String name=“nihar”; Name.equals(“nihar”);

equalsIgnoreCase()

equals compares for contents

==  compares for two object references, to see

(15)

Example

String s1=“java” String s2=“JAVA”;

(16)

toString()

Class Student {String name; int roll

}

Student Obj=new Student(); System.out.println(Obj) ;

 what is the output?;

Class Student {….

…..

public String toString() {

System.out.println(“Student@”+name+”@”+”roll”); }

(17)

StringBuffer

String class objects are immutable and with fixed length in contrast to StringBuffer class objects whose length can be varied and we can insert characters either in the middle or at the end

Constructor

StringBuffer() 16 char

StringBuffer(int i)

(18)

Principal methods

append

insert

length

capacity

reverse

StringBuffer name=new

StringBuffer(“Nihar”); name.append(“ Roy”);

(19)

Append example

class StrBuffer {

public static void main(String args[]) {

StringBuffer str=new StringBuffer(10); str.append("India is");

System.out.println(str);

System.out.println(str.length()); System.out.println(str.capacity()); str.append("a beautiful country"); System.out.println(str.length()); System.out.println(str.capacity()); }

(20)

Arrays

Collection of elements of the same type

that are referred by a common name.

int arr=new int[5];

All initialise with default value

(21)

MultiDimensional

int arr[][]=new int[4][4];

Initialisation

Arr[0][0]=45;

Arr[0][1]=34;

….

Or

(22)

Vector

package import java.util.*;

It permits to store different elements that are of different class.

• Vector in also an expandable array of objects

• Array can be either increased of decreased after

some its elements have been deleted

• We cannot use simple data types in vector

• Vector automatically increased its size when

(23)

Constructor

Vector()

Vector(int size)

Vector(int size, int increment)

The value is incremented whenever it

(24)

Methods in Vector class

addElement()at the last position

elementAt(int i)returns element at that position

Size()no of objects present

Capacityno of spaces allocated

removeElement(Object o)removes the first

occurrence of the object o in the vectore if and only if it is found

(25)

How to add primitive data types

Vector v=new Vector(2,4);

v.addElement(new Integer(1200));

v.addElement(new Character(‘N’));

v.addElement(new Boolen(true));

References

Related documents

To study the in fluence of various design constraints (eigenfrequency, extreme load and fatigue load constraints) on the structural design and the optimization procedure2. Section

Bicolored or uniformly dark, with microsculpture mesh pattern slightly transverse, sculpticells slightly wider than long (Fig. 5C-D) in dorsal aspect slender, shaft subsinuate,

[r]

He is currently a Lecturer in Games Design and Development in the School of Electronics and Info-Comm Technology, ITE College Central (Singapore), and an Adjunct

public Socket(String host, int port, InetAddress localAddress, int localPort) throws IOException5. public Socket(InetAddress address, int port, InetAddress localAddress, int

Europe of the regions, along with the movement for a European Union and for multiculturalism reject common nationality as the common bond on which political units must be based..

1.5 Safety Practices during Scientific Investigations.. laboratory) for  Choosing a site (e.g. laboratory) for  investigation depends on factors such investigation depends on

During the management of the Covid-19 in Turkey, officials of the Ministry of Health, members of the Covid-19 scientific board (summoned from the leading physicians) and