J6-ARRAYS & String Handling
J6-ARRAYS & String Handling
Dr B.Veera Jyothi
Assistant Professor
IT Department,CBIT.
ARRAYS: HOW TO CREATE AND DEFINE ARRAYS
Introduction
Array declaration
Array construction
Array initialization
Java Programs
ARRAYS: HOW TO CREATE AND DEFINE ARRAYS
Arrays are fixed in size .Arrays are useful if we know the size
in advance.
int[] a; //valid
int[5] a; //invalid
ARRAYS: HOW TO CREATE AND DEFINE ARRAYS
Two dimensional array declaration:
Example:
int[][] a;
int [][]a;
int[] []a;
int[] a[];
int []a[];
WHAT IS THE OUTPUT OF THE FOLLOWING CODE?
public class Test{
public static void main(String args[])
{
double[] myList = {1, 5, 5, 5, 5, 1};
double max = myList[0];
int indexOfMax = 0;
for(int i = 1; i < myList.length; i++)
{
if(myList[i] > max)
{ max = myList[i];
indexOfMax = i;
}
}
System.out.println(indexOfMax);
}
}
Example:
int[] a=new int[3];
For every array type corresponding classes are available but these
classes are part of java language and not available to the programmer
level.
What is the result of compiling and running the
following code?
public class Test{
public static void main(String[] args)
{
int[] a = new int[0];
System.out.print(a.length);
}
}
A. 0
B. Compilation error, arrays cannot be initialized to zero
size.
C. Compilation error, it is a.length() not a.length
D. None of the above
ARRAYS: HOW TO CREATE AND DEFINE ARRAYS
ARRAYS: HOW TO CREATE AND DEFINE ARRAYS
Multi dimensional array creation:
In java multidimensional arrays are implemented as array of
arrays approach but not matrix form.
Advantage of this approach is to improve memory utilization.
Example 1:
int[][] a =new int[2][];
a[0]=new int[3];
a[1]=new int[2];
Memory Representation
ARRAYS: HOW TO CREATE AND DEFINE ARRAYS
Whenever we are creating an array every element is initialized
with default value automatically.
Example:
int[] a=new int[3];
System.out.println(a); //[I@3e25a5
System.out.println(a[0]); //0
Note:
Whenever we are trying to print any object reference internally
toString() method will be executed which is implemented by
default to return the following.
DECLARATION, CONSTRUCTION AND INITIALIZATION OF AN
ARRAY IN A SINGLE LINE
We can perform declaration, construction and initialization of an
array in a single line.
Example:
String[] s={“ECE”,”IT",”EEE",”MECH"};//valid
ARRAYS: HOW TO CREATE AND DEFINE ARRAYS
Which one of the following is a valid statement?
A. char[] c = new char();
B. char[] c = new char[5];
C. char[] c = new char(4);
D. char[] c = new char[];
JAVA.LANG.OBJECT CLASS
The Object class is the parent class of all the classes in java by
default. In other words, it is the topmost class of java.
JAVA.LANG.OBJECT CLASS
JAVA STRING CLASS
Generally, String is a sequence of characters. But in Java,
string is an object that represents a sequence of characters. The
java.lang.String class is used to create a string object.
There are two ways to create String object:
By string literal
By new keyword
String Literal
String s1=“CBIT";
String s2=“CBIT";
JAVA STRING CLASS
By new keyword
String s=new String("Welcome");
String class constructors :
String s=new String();
Creates an empty String Object.
String s=new String(String literals);
To create an equivalent String object for the given String literal
on the heap.
String s=new String(StringBuffer sb);
Creates an equivalent String object for the given StringBuffer.
JAVA STRING CLASS
String s=new String(char[] ch);
creates an equivalent String object for the given char[ ] array.
Answer the Following :
If S1 and S2 are two strings, which of the following
statements or expressions are correct.
i) String S3=S1+S2;
ii) String S3=S1-S2;
iii) S1<=S2;
iv) S1.compareTo(S2);
A) i and ii only
B) ii and iii only
C) ii and iv only
D) i and iv only
JAVA STRING CLASS
String s=new String(byte[] b);
Create an equivalent String object for the given byte[] array.
JAVA STRING BUFFER CLASS
Java StringBuffer class is used to create mutable (modifiable)
string. The StringBuffer class in java is same as String class
except it is mutable i.e. it can be changed.
StringBuffer(): creates an empty string buffer with the
initial capacity of 16.
StringBuffer(String str): creates a string buffer with the
specified string.
StringBuffer(int capacity): creates an empty string buffer
with the specified capacity as length.
STRING VS STRING BUFFER
Object equals()
String StringBuffer
equals()
What is the string contained in s after following lines of
code?
StringBuffer s= new StringBuffer("Hello”);
s.deleteCharAt(0);
A. Hell
B. ello
C. Hel
D. llo
JAVA STRING BUFFER CLASS
String s=new string(“CBIT”);
System.out.println(s.concat(“ECE”));
s.append(“ECE”);
System.out.println(s);
JAVA STRING BUFFER CLASS
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
STRINGTOKENIZER CLASS IN JAVA
The java.util.StringTokenizer class allows you to break a
string into tokens. It is simple way to break string.
Constructors of StringTokenizer class