SlideShare a Scribd company logo
Arrays 
String Handling 
Java Packages 
Lecture - 4 
Dr. Shahid Raza Spring,2014
Creating Arrays" 
• Creating an array is a 2 step process" 
– It must be declared (declaration does not specify size)" 
" 
" 
declaration syntax:" 
type[] arrayName;! 
! 
" 
" 
note the location of the []" 
" 
– It must be created (ie. memory must be allocated for the array)" 
" 
int[] grades; ! ! !// declaration! 
! 
grades = new int[5]; ! !// Create array. ! 
! ! ! ! !// specify size! 
! ! ! ! !// assign new array to ! 
! ! ! ! !// array variable! 
!
Using initializer lists" 
• Another way of initializing lists is by using initializer lists." 
– The array is automatically created" 
– The array size is computed from the number of items in the list." 
" 
type[] arrayName = {initializer_list};! 
! 
int[] grades = {100, 96, 78, 86, 93};! 
String[] colours = { !"Red", "Orange",! 
! ! ! !"Yellow", "Green",! 
! ! ! !"Blue", "Indigo",! 
! ! ! !"Violet"}; !
The main() method" 
• You may recall that the main method takes an array of 
String objects as a parameter." 
– This array of Strings holds the command line parameters which were 
passed to the java program when it was started" 
" 
public class HelloWorld! 
{! 
!public static void main(String[] args)! 
!{! 
! !System.out.println("Hello World");! 
!}! 
}! 
Array holding command line parameters"
Command line parameters" 
name of class containing the main() method" 
0" 
1" 
2" 
3" 
4" 
java HelloWorld This is a test, Jim" 
args" 
This" 
is" 
Jim" 
a" 
test,"
Multi-dimensional Arrays" 
• Arrays with multiple dimensions can also be created." 
" 
declaration syntax:" " 
type[][] arrayName;! 
" 
" 
each [] indicates another dimension" 
" 
• They are created and initialized in the same way as 
single dimensioned arrays." 
" 
int[][] grades = new int[20][5];! 
!! 
String[][] colours = !{{"Red", "Green", "Blue"},! 
! ! ! ! {"Cyan", "Magenta", "Yellow"},! 
! ! ! ! {"Russet", "Mauve", "Orange"}};!
Strings 
• Java string is a sequence of characters. They are objects of 
type String. 
• Once a String object is created it cannot be changed. Stings 
are Immutable. 
• To get changeable strings use the class called StringBuffer. 
• String and StringBuffer classes are declared final, so there 
cannot be subclasses of these classes. 
• The default constructor creates an empty string. 
String s = new String(); 
Dr. Shahid Raza MPL
Creating Strings 
• String str = "abc"; is equivalent to: 
char data[] = {'a', 'b', 'c'}; 
String str = new String(data); 
• If data array in the above example is modified after the 
string object str is created, then str remains unchanged. 
• Construct a string object by passing another string object. 
String str2 = new String(str); 
Dr. Shahid Raza MPL
String Operations 
• The length() method returns the length of the string. 
Eg: System.out.println(“Hello”.length()); // prints 5 
• The + operator is used to concatenate two or more strings. 
Eg: String myname = “Harry” 
String str = “My name is” + myname+ “.”; 
• For string concatenation the Java compiler converts an 
operand to a String whenever the other operand of the + is 
a String object. 
Dr. Shahid Raza MPL
String Operations 
• Characters in a string can be extracted in a number of 
ways. 
public char charAt(int index) 
– Returns the character at the specified index. An index 
ranges from 0 to length() - 1. The first character of the 
sequence is at index 0, the next at index 1, and so on, as 
for array indexing. 
char ch; 
ch = “abc”.charAt(1); // ch = “b” 
Dr. Shahid Raza MPL
String Operations 
• getChars() - Copies characters from this string into the 
destination character array. 
public void getChars(int srcBegin, int srcEnd, 
char[] dst, int dstBegin) 
– srcBegin - index of the first character in the string to copy. 
– srcEnd - index after the last character in the string to copy. 
– dst - the destination array. 
– dstBegin - the start offset in the destination array. 
Dr. Shahid Raza MPL
String Operations 
• equals() - Compares the invoking string to the specified object. The 
result is true if and only if the argument is not null and is a String 
object that represents the same sequence of characters as the invoking 
object. 
public boolean equals(Object anObject) 
• equalsIgnoreCase()- Compares this String to another String, ignoring 
case considerations. Two strings are considered equal ignoring case if 
they are of the same length, and corresponding characters in the two 
strings are equal ignoring case. 
public boolean equalsIgnoreCase(String 
anotherString) 
Dr. Shahid Raza MPL
String Operations 
• startsWith() – Tests if this string starts with the specified 
prefix. 
public boolean startsWith(String prefix) 
“Figure”.startsWith(“Fig”); // true 
• endsWith() - Tests if this string ends with the specified 
suffix. 
public boolean endsWith(String suffix) 
“Figure”.endsWith(“re”); // true 
Dr. Shahid Raza MPL
String Operations 
• startsWith() -Tests if this string starts with the specified 
prefix beginning at a specified index. 
public boolean startsWith(String prefix, 
int toffset) 
prefix - the prefix. 
toffset - where to begin looking in the 
string. 
“figure”.startsWith(“gure”, 2); // true 
Dr. Shahid Raza MPL
String Operations 
• compareTo() - Compares two strings lexicographically. 
– The result is a negative integer if this String object 
lexicographically precedes the argument string. 
– The result is a positive integer if this String object 
lexicographically follows the argument string. 
– The result is zero if the strings are equal. 
– compareTo returns 0 exactly when the equals(Object) method 
would return true. 
public int compareTo(String anotherString) 
public int compareToIgnoreCase(String str) 
Dr. Shahid Raza MPL
String Operations 
indexOf – Searches for the first occurrence of a character or substring. 
Returns -1 if the character does not occur. 
public int indexOf(int ch)- Returns the index within this 
string of the first occurrence of the specified character. 
public int indexOf(String str) - Returns the index 
within this string of the first occurrence of the specified substring. 
String str = “How was your day today?”; 
str.indexof(‘t’); 
str.indexof(“was”); 
Dr. Shahid Raza MPL
String Operations 
public 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. 
public 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. 
String str = “How was your day today?”; 
str.indexof(‘a’, 6); 
str(“was”, 2); 
Dr. Shahid Raza MPL
String Operations 
lastIndexOf() –Searches for the last occurrence of a character 
or substring. The methods are similar to indexOf(). 
substring() - Returns a new string that is a substring of this 
string. The substring begins with the character at the 
specified index and extends to the end of this string. 
public String substring(int beginIndex) 
Eg: "unhappy".substring(2) returns "happy" 
Dr. Shahid Raza MPL
String Operations 
• public String 
substring(int beginIndex, 
int endIndex) 
Eg: "smiles".substring(1, 5) 
returns "mile“ 
Dr. Shahid Raza MPL
String Operations 
concat() - Concatenates the specified string to the end of this 
string. 
If the length of the argument string is 0, then this String 
object is returned. 
Otherwise, a new String object is created, containing the 
invoking string with the contents of the str appended to it. 
public String concat(String str) 
"to".concat("get").concat("her") returns 
"together" 
Dr. Shahid Raza MPL
String Operations 
• replace()- Returns a new string resulting from replacing all 
occurrences of oldChar in this string with newChar. 
public String replace(char oldChar, char newChar) 
"mesquite in your cellar".replace('e', 'o') 
returns "mosquito in your collar" 
Dr. Shahid Raza MPL
String Operations 
• trim() - Returns a copy of the string, with leading and 
trailing whitespace omitted. 
public String trim() 
String s = “ Hi Mom! “.trim(); 
S = “Hi Mom!” 
• valueOf() – Returns the string representation of the char 
array argument. 
public static String valueOf(char[] data) 
Dr. Shahid Raza MPL
String Operations 
• The contents of the character array are copied; subsequent 
modification of the character array does not affect the 
newly created string. 
Other forms are: 
public static String valueOf(char c) 
public static String valueOf(boolean b) 
public static String valueOf(int i) 
public static String valueOf(long l) 
public static String valueOf(float f) 
public static String valueOf(double d) 
Dr. Shahid Raza MPL
String Operations 
• toLowerCase(): Converts all of the characters in a String 
to lower case. 
• toUpperCase(): Converts all of the characters in this 
String to upper case. 
public String toLowerCase() 
public String toUpperCase() 
Eg: “HELLO THERE”.toLowerCase(); 
“hello there”.toUpperCase(); 
Dr. Shahid Raza MPL
StringBuffer 
• A StringBuffer is like a String, but can be modified. 
• The length and content of the StringBuffer sequence can 
be changed through certain method calls. 
• StringBuffer defines three constructors: 
– StringBuffer() 
– StringBuffer(int size) 
– StringBuffer(String str) 
Dr. Shahid Raza MPL
StringBuffer Operations 
• The principal operations on a StringBuffer are the append 
and insert methods, which are overloaded so as to accept 
data of any type. 
Here are few append methods: 
StringBuffer append(String str) 
StringBuffer append(int num) 
• The append method always adds these characters at the 
end of the buffer. 
Dr. Shahid Raza MPL
StringBuffer Operations 
• The insert method adds the characters at a specified point. 
Here are few insert methods: 
StringBuffer insert(int index, String str) 
StringBuffer append(int index, char ch) 
Index specifies at which point the string will be inserted into 
the invoking StringBuffer object. 
Dr. Shahid Raza MPL
StringBuffer Operations 
• delete() - Removes the characters in a substring of this 
StringBuffer. The substring begins at the specified start 
and extends to the character at index end - 1 or to the end 
of the StringBuffer if no such character exists. If start is 
equal to end, no changes are made. 
public StringBuffer delete(int start, int end) 
Dr. Shahid Raza MPL
StringBuffer Operations 
• replace() - Replaces the characters in a substring of this 
StringBuffer with characters in the specified String. 
public StringBuffer replace(int start, int end, 
String str) 
• substring() - Returns a new String that contains a 
subsequence of characters currently contained in this 
StringBuffer. The substring begins at the specified index 
and extends to the end of the StringBuffer. 
public String substring(int start) 
Dr. Shahid Raza MPL
StringBuffer Operations 
• reverse() - The character sequence contained in this string 
buffer is replaced by the reverse of the sequence. 
public StringBuffer reverse() 
• length() - Returns the length of this string buffer. 
public int length() 
Dr. Shahid Raza MPL
StringBuffer Operations 
• capacity() - Returns the current capacity of the String 
buffer. The capacity is the amount of storage available for 
newly inserted characters. 
public int capacity() 
• charAt() - The specified character of the sequence 
currently represented by the string buffer, as indicated by 
the index argument, is returned. 
public char charAt(int index) 
Dr. Shahid Raza MPL
Examples: StringBuffer 
StringBuffer sb = new StringBuffer(“Hello”); 
sb.length(); // 5 
sb.capacity(); // 21 (16 characters room is 
added if no size is specified) 
sb.charAt(1); // e 
sb.setCharAt(1,’i’); // Hillo 
sb.setLength(2); // Hi 
sb.insert(0, “Big “); // Big Hill 
Dr. Shahid Raza MPL

More Related Content

What's hot

String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
Anwar Hasan Shuvo
 
Strings
StringsStrings
Java String
Java String Java String
Java String
SATYAM SHRIVASTAV
 
Java string handling
Java string handlingJava string handling
Java string handling
Salman Khan
 
String Handling
String HandlingString Handling
String Handling
Bharat17485
 
Chapter 7 String
Chapter 7 StringChapter 7 String
Chapter 7 String
OUM SAOKOSAL
 
Java String
Java StringJava String
Java String
Java2Blog
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
Abhilash Nair
 
Java strings
Java   stringsJava   strings
Java strings
Mohammed Sikander
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
teach4uin
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
Victer Paul
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
Java string , string buffer and wrapper class
Java string , string buffer and wrapper classJava string , string buffer and wrapper class
Java string , string buffer and wrapper class
SimoniShah6
 
What is String in Java?
What is String in Java?What is String in Java?
What is String in Java?
RAKESH P
 
String java
String javaString java
String java
774474
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
Ravi Kant Sahu
 
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Edureka!
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
Ravi Kant Sahu
 
JVM and OOPS Introduction
JVM and OOPS IntroductionJVM and OOPS Introduction
JVM and OOPS Introduction
SATYAM SHRIVASTAV
 
Autoboxing And Unboxing In Java
Autoboxing And Unboxing In JavaAutoboxing And Unboxing In Java
Autoboxing And Unboxing In Java
chathuranga kasun bamunusingha
 

What's hot (20)

String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 
Strings
StringsStrings
Strings
 
Java String
Java String Java String
Java String
 
Java string handling
Java string handlingJava string handling
Java string handling
 
String Handling
String HandlingString Handling
String Handling
 
Chapter 7 String
Chapter 7 StringChapter 7 String
Chapter 7 String
 
Java String
Java StringJava String
Java String
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Java strings
Java   stringsJava   strings
Java strings
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Java string , string buffer and wrapper class
Java string , string buffer and wrapper classJava string , string buffer and wrapper class
Java string , string buffer and wrapper class
 
What is String in Java?
What is String in Java?What is String in Java?
What is String in Java?
 
String java
String javaString java
String java
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
JVM and OOPS Introduction
JVM and OOPS IntroductionJVM and OOPS Introduction
JVM and OOPS Introduction
 
Autoboxing And Unboxing In Java
Autoboxing And Unboxing In JavaAutoboxing And Unboxing In Java
Autoboxing And Unboxing In Java
 

Viewers also liked

Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
Princess Sam
 
April 2014
April 2014April 2014
April 2014
Siddharth Pereira
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part V
Hari Christian
 
Indian Property Lawyers .com - How It Works
Indian Property Lawyers .com - How It WorksIndian Property Lawyers .com - How It Works
Indian Property Lawyers .com - How It Works
buzzfactory
 
Java operators
Java operatorsJava operators
Java operators
Shehrevar Davierwala
 
Java arrays
Java arraysJava arrays
Java arrays
BHUVIJAYAVELU
 
L11 array list
L11 array listL11 array list
L11 array list
teach4uin
 
String handling in_java
String handling in_javaString handling in_java
String handling in_java
774474
 
Java packages
Java packagesJava packages
Java packages
Shreyans Pathak
 
Indian food jnv bhiwani
Indian food jnv bhiwaniIndian food jnv bhiwani
Indian food jnv bhiwani
vermanamita
 
Arrays (vetores) em Java
Arrays (vetores) em JavaArrays (vetores) em Java
Arrays (vetores) em Java
Daniel Brandão
 
Packages
PackagesPackages
Packages
Ravi_Kant_Sahu
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Ganesh Samarthyam
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
Jussi Pohjolainen
 
java packages
java packagesjava packages
java packages
aptechsravan
 
Amazing INDIA
Amazing INDIAAmazing INDIA
Amazing INDIA
Nubia **
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
kamal kotecha
 
LAB07 ARRAYS JAVA BOOT CAMP!
LAB07 ARRAYS JAVA BOOT CAMP!LAB07 ARRAYS JAVA BOOT CAMP!
LAB07 ARRAYS JAVA BOOT CAMP!
A Jorge Garcia
 
Packages in java
Packages in javaPackages in java
Packages in java
Abhishek Khune
 

Viewers also liked (19)

Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
April 2014
April 2014April 2014
April 2014
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part V
 
Indian Property Lawyers .com - How It Works
Indian Property Lawyers .com - How It WorksIndian Property Lawyers .com - How It Works
Indian Property Lawyers .com - How It Works
 
Java operators
Java operatorsJava operators
Java operators
 
Java arrays
Java arraysJava arrays
Java arrays
 
L11 array list
L11 array listL11 array list
L11 array list
 
String handling in_java
String handling in_javaString handling in_java
String handling in_java
 
Java packages
Java packagesJava packages
Java packages
 
Indian food jnv bhiwani
Indian food jnv bhiwaniIndian food jnv bhiwani
Indian food jnv bhiwani
 
Arrays (vetores) em Java
Arrays (vetores) em JavaArrays (vetores) em Java
Arrays (vetores) em Java
 
Packages
PackagesPackages
Packages
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
java packages
java packagesjava packages
java packages
 
Amazing INDIA
Amazing INDIAAmazing INDIA
Amazing INDIA
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
LAB07 ARRAYS JAVA BOOT CAMP!
LAB07 ARRAYS JAVA BOOT CAMP!LAB07 ARRAYS JAVA BOOT CAMP!
LAB07 ARRAYS JAVA BOOT CAMP!
 
Packages in java
Packages in javaPackages in java
Packages in java
 

Similar to Arrays string handling java packages

Strings in java
Strings in javaStrings in java
Strings in java
Kuppusamy P
 
Module-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdfModule-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
Shahjahan Samoon
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation x
Shahjahan Samoon
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
Infoviaan Technologies
 
Java string handling
Java string handlingJava string handling
Java string handling
GaneshKumarKanthiah
 
Strings.ppt
Strings.pptStrings.ppt
Strings.ppt
SanthiyaAK
 
13 Strings and text processing
13 Strings and text processing13 Strings and text processing
13 Strings and text processing
maznabili
 
8. String
8. String8. String
8. String
Nilesh Dalvi
 
07slide
07slide07slide
07slide
Aboudi Sabbah
 
Text processing
Text processingText processing
Text processing
Icancode
 
Charcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.pptCharcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
Day_5.1.pptx
Day_5.1.pptxDay_5.1.pptx
Day_5.1.pptx
ishasharma835109
 
M C6java7
M C6java7M C6java7
M C6java7
mbruggen
 
Lecture 7
Lecture 7Lecture 7
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
SatyajeetGaur3
 
Computer programming 2 Lesson 12
Computer programming 2  Lesson 12Computer programming 2  Lesson 12
Computer programming 2 Lesson 12
MLG College of Learning, Inc
 
Strings in python
Strings in pythonStrings in python
Strings in python
Prabhakaran V M
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
SachinBhosale73
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
Terry Yoast
 

Similar to Arrays string handling java packages (20)

Strings in java
Strings in javaStrings in java
Strings in java
 
Module-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdfModule-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdf
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation x
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Strings.ppt
Strings.pptStrings.ppt
Strings.ppt
 
13 Strings and text processing
13 Strings and text processing13 Strings and text processing
13 Strings and text processing
 
8. String
8. String8. String
8. String
 
07slide
07slide07slide
07slide
 
Text processing
Text processingText processing
Text processing
 
Charcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.pptCharcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.ppt
 
Day_5.1.pptx
Day_5.1.pptxDay_5.1.pptx
Day_5.1.pptx
 
M C6java7
M C6java7M C6java7
M C6java7
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
 
Computer programming 2 Lesson 12
Computer programming 2  Lesson 12Computer programming 2  Lesson 12
Computer programming 2 Lesson 12
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 

More from Sardar Alam

Undoing of mental illness -- seek help
Undoing of mental illness -- seek helpUndoing of mental illness -- seek help
Undoing of mental illness -- seek help
Sardar Alam
 
introduction to python
introduction to pythonintroduction to python
introduction to python
Sardar Alam
 
Operator Overloading
Operator Overloading  Operator Overloading
Operator Overloading
Sardar Alam
 
skin disease classification
skin disease classificationskin disease classification
skin disease classification
Sardar Alam
 
filters for noise in image processing
filters for noise in image processingfilters for noise in image processing
filters for noise in image processing
Sardar Alam
 
Noise Models
Noise ModelsNoise Models
Noise Models
Sardar Alam
 
Noise Models
Noise ModelsNoise Models
Noise Models
Sardar Alam
 
Introduction to machine learningunsupervised learning
Introduction to machine learningunsupervised learningIntroduction to machine learningunsupervised learning
Introduction to machine learningunsupervised learning
Sardar Alam
 
Opengl texturing
Opengl texturingOpengl texturing
Opengl texturing
Sardar Alam
 
Mathematics fundamentals
Mathematics fundamentalsMathematics fundamentals
Mathematics fundamentals
Sardar Alam
 
3 d graphics with opengl part 2
3 d graphics with opengl  part 23 d graphics with opengl  part 2
3 d graphics with opengl part 2
Sardar Alam
 
3 d graphics with opengl part 1
3 d graphics with opengl part 13 d graphics with opengl part 1
3 d graphics with opengl part 1
Sardar Alam
 
3 d graphics basics
3 d graphics basics3 d graphics basics
3 d graphics basics
Sardar Alam
 
2 d transformations
2 d transformations2 d transformations
2 d transformations
Sardar Alam
 
Gui
GuiGui
Inheritance
InheritanceInheritance
Inheritance
Sardar Alam
 
Java basics
Java basicsJava basics
Java basics
Sardar Alam
 

More from Sardar Alam (17)

Undoing of mental illness -- seek help
Undoing of mental illness -- seek helpUndoing of mental illness -- seek help
Undoing of mental illness -- seek help
 
introduction to python
introduction to pythonintroduction to python
introduction to python
 
Operator Overloading
Operator Overloading  Operator Overloading
Operator Overloading
 
skin disease classification
skin disease classificationskin disease classification
skin disease classification
 
filters for noise in image processing
filters for noise in image processingfilters for noise in image processing
filters for noise in image processing
 
Noise Models
Noise ModelsNoise Models
Noise Models
 
Noise Models
Noise ModelsNoise Models
Noise Models
 
Introduction to machine learningunsupervised learning
Introduction to machine learningunsupervised learningIntroduction to machine learningunsupervised learning
Introduction to machine learningunsupervised learning
 
Opengl texturing
Opengl texturingOpengl texturing
Opengl texturing
 
Mathematics fundamentals
Mathematics fundamentalsMathematics fundamentals
Mathematics fundamentals
 
3 d graphics with opengl part 2
3 d graphics with opengl  part 23 d graphics with opengl  part 2
3 d graphics with opengl part 2
 
3 d graphics with opengl part 1
3 d graphics with opengl part 13 d graphics with opengl part 1
3 d graphics with opengl part 1
 
3 d graphics basics
3 d graphics basics3 d graphics basics
3 d graphics basics
 
2 d transformations
2 d transformations2 d transformations
2 d transformations
 
Gui
GuiGui
Gui
 
Inheritance
InheritanceInheritance
Inheritance
 
Java basics
Java basicsJava basics
Java basics
 

Recently uploaded

Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
aymanquadri279
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
Hironori Washizaki
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 

Recently uploaded (20)

Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 

Arrays string handling java packages

  • 1. Arrays String Handling Java Packages Lecture - 4 Dr. Shahid Raza Spring,2014
  • 2. Creating Arrays" • Creating an array is a 2 step process" – It must be declared (declaration does not specify size)" " " declaration syntax:" type[] arrayName;! ! " " note the location of the []" " – It must be created (ie. memory must be allocated for the array)" " int[] grades; ! ! !// declaration! ! grades = new int[5]; ! !// Create array. ! ! ! ! ! !// specify size! ! ! ! ! !// assign new array to ! ! ! ! ! !// array variable! !
  • 3. Using initializer lists" • Another way of initializing lists is by using initializer lists." – The array is automatically created" – The array size is computed from the number of items in the list." " type[] arrayName = {initializer_list};! ! int[] grades = {100, 96, 78, 86, 93};! String[] colours = { !"Red", "Orange",! ! ! ! !"Yellow", "Green",! ! ! ! !"Blue", "Indigo",! ! ! ! !"Violet"}; !
  • 4. The main() method" • You may recall that the main method takes an array of String objects as a parameter." – This array of Strings holds the command line parameters which were passed to the java program when it was started" " public class HelloWorld! {! !public static void main(String[] args)! !{! ! !System.out.println("Hello World");! !}! }! Array holding command line parameters"
  • 5. Command line parameters" name of class containing the main() method" 0" 1" 2" 3" 4" java HelloWorld This is a test, Jim" args" This" is" Jim" a" test,"
  • 6. Multi-dimensional Arrays" • Arrays with multiple dimensions can also be created." " declaration syntax:" " type[][] arrayName;! " " each [] indicates another dimension" " • They are created and initialized in the same way as single dimensioned arrays." " int[][] grades = new int[20][5];! !! String[][] colours = !{{"Red", "Green", "Blue"},! ! ! ! ! {"Cyan", "Magenta", "Yellow"},! ! ! ! ! {"Russet", "Mauve", "Orange"}};!
  • 7. Strings • Java string is a sequence of characters. They are objects of type String. • Once a String object is created it cannot be changed. Stings are Immutable. • To get changeable strings use the class called StringBuffer. • String and StringBuffer classes are declared final, so there cannot be subclasses of these classes. • The default constructor creates an empty string. String s = new String(); Dr. Shahid Raza MPL
  • 8. Creating Strings • String str = "abc"; is equivalent to: char data[] = {'a', 'b', 'c'}; String str = new String(data); • If data array in the above example is modified after the string object str is created, then str remains unchanged. • Construct a string object by passing another string object. String str2 = new String(str); Dr. Shahid Raza MPL
  • 9. String Operations • The length() method returns the length of the string. Eg: System.out.println(“Hello”.length()); // prints 5 • The + operator is used to concatenate two or more strings. Eg: String myname = “Harry” String str = “My name is” + myname+ “.”; • For string concatenation the Java compiler converts an operand to a String whenever the other operand of the + is a String object. Dr. Shahid Raza MPL
  • 10. String Operations • Characters in a string can be extracted in a number of ways. public char charAt(int index) – Returns the character at the specified index. An index ranges from 0 to length() - 1. The first character of the sequence is at index 0, the next at index 1, and so on, as for array indexing. char ch; ch = “abc”.charAt(1); // ch = “b” Dr. Shahid Raza MPL
  • 11. String Operations • getChars() - Copies characters from this string into the destination character array. public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) – srcBegin - index of the first character in the string to copy. – srcEnd - index after the last character in the string to copy. – dst - the destination array. – dstBegin - the start offset in the destination array. Dr. Shahid Raza MPL
  • 12. String Operations • equals() - Compares the invoking string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as the invoking object. public boolean equals(Object anObject) • equalsIgnoreCase()- Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length, and corresponding characters in the two strings are equal ignoring case. public boolean equalsIgnoreCase(String anotherString) Dr. Shahid Raza MPL
  • 13. String Operations • startsWith() – Tests if this string starts with the specified prefix. public boolean startsWith(String prefix) “Figure”.startsWith(“Fig”); // true • endsWith() - Tests if this string ends with the specified suffix. public boolean endsWith(String suffix) “Figure”.endsWith(“re”); // true Dr. Shahid Raza MPL
  • 14. String Operations • startsWith() -Tests if this string starts with the specified prefix beginning at a specified index. public boolean startsWith(String prefix, int toffset) prefix - the prefix. toffset - where to begin looking in the string. “figure”.startsWith(“gure”, 2); // true Dr. Shahid Raza MPL
  • 15. String Operations • compareTo() - Compares two strings lexicographically. – The result is a negative integer if this String object lexicographically precedes the argument string. – The result is a positive integer if this String object lexicographically follows the argument string. – The result is zero if the strings are equal. – compareTo returns 0 exactly when the equals(Object) method would return true. public int compareTo(String anotherString) public int compareToIgnoreCase(String str) Dr. Shahid Raza MPL
  • 16. String Operations indexOf – Searches for the first occurrence of a character or substring. Returns -1 if the character does not occur. public int indexOf(int ch)- Returns the index within this string of the first occurrence of the specified character. public int indexOf(String str) - Returns the index within this string of the first occurrence of the specified substring. String str = “How was your day today?”; str.indexof(‘t’); str.indexof(“was”); Dr. Shahid Raza MPL
  • 17. String Operations public 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. public 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. String str = “How was your day today?”; str.indexof(‘a’, 6); str(“was”, 2); Dr. Shahid Raza MPL
  • 18. String Operations lastIndexOf() –Searches for the last occurrence of a character or substring. The methods are similar to indexOf(). substring() - Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string. public String substring(int beginIndex) Eg: "unhappy".substring(2) returns "happy" Dr. Shahid Raza MPL
  • 19. String Operations • public String substring(int beginIndex, int endIndex) Eg: "smiles".substring(1, 5) returns "mile“ Dr. Shahid Raza MPL
  • 20. String Operations concat() - Concatenates the specified string to the end of this string. If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created, containing the invoking string with the contents of the str appended to it. public String concat(String str) "to".concat("get").concat("her") returns "together" Dr. Shahid Raza MPL
  • 21. String Operations • replace()- Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. public String replace(char oldChar, char newChar) "mesquite in your cellar".replace('e', 'o') returns "mosquito in your collar" Dr. Shahid Raza MPL
  • 22. String Operations • trim() - Returns a copy of the string, with leading and trailing whitespace omitted. public String trim() String s = “ Hi Mom! “.trim(); S = “Hi Mom!” • valueOf() – Returns the string representation of the char array argument. public static String valueOf(char[] data) Dr. Shahid Raza MPL
  • 23. String Operations • The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string. Other forms are: public static String valueOf(char c) public static String valueOf(boolean b) public static String valueOf(int i) public static String valueOf(long l) public static String valueOf(float f) public static String valueOf(double d) Dr. Shahid Raza MPL
  • 24. String Operations • toLowerCase(): Converts all of the characters in a String to lower case. • toUpperCase(): Converts all of the characters in this String to upper case. public String toLowerCase() public String toUpperCase() Eg: “HELLO THERE”.toLowerCase(); “hello there”.toUpperCase(); Dr. Shahid Raza MPL
  • 25. StringBuffer • A StringBuffer is like a String, but can be modified. • The length and content of the StringBuffer sequence can be changed through certain method calls. • StringBuffer defines three constructors: – StringBuffer() – StringBuffer(int size) – StringBuffer(String str) Dr. Shahid Raza MPL
  • 26. StringBuffer Operations • The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Here are few append methods: StringBuffer append(String str) StringBuffer append(int num) • The append method always adds these characters at the end of the buffer. Dr. Shahid Raza MPL
  • 27. StringBuffer Operations • The insert method adds the characters at a specified point. Here are few insert methods: StringBuffer insert(int index, String str) StringBuffer append(int index, char ch) Index specifies at which point the string will be inserted into the invoking StringBuffer object. Dr. Shahid Raza MPL
  • 28. StringBuffer Operations • delete() - Removes the characters in a substring of this StringBuffer. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the StringBuffer if no such character exists. If start is equal to end, no changes are made. public StringBuffer delete(int start, int end) Dr. Shahid Raza MPL
  • 29. StringBuffer Operations • replace() - Replaces the characters in a substring of this StringBuffer with characters in the specified String. public StringBuffer replace(int start, int end, String str) • substring() - Returns a new String that contains a subsequence of characters currently contained in this StringBuffer. The substring begins at the specified index and extends to the end of the StringBuffer. public String substring(int start) Dr. Shahid Raza MPL
  • 30. StringBuffer Operations • reverse() - The character sequence contained in this string buffer is replaced by the reverse of the sequence. public StringBuffer reverse() • length() - Returns the length of this string buffer. public int length() Dr. Shahid Raza MPL
  • 31. StringBuffer Operations • capacity() - Returns the current capacity of the String buffer. The capacity is the amount of storage available for newly inserted characters. public int capacity() • charAt() - The specified character of the sequence currently represented by the string buffer, as indicated by the index argument, is returned. public char charAt(int index) Dr. Shahid Raza MPL
  • 32. Examples: StringBuffer StringBuffer sb = new StringBuffer(“Hello”); sb.length(); // 5 sb.capacity(); // 21 (16 characters room is added if no size is specified) sb.charAt(1); // e sb.setCharAt(1,’i’); // Hillo sb.setLength(2); // Hi sb.insert(0, “Big “); // Big Hill Dr. Shahid Raza MPL