0% found this document useful (0 votes)
72 views

J6-ARRAYS & String Handling

This document discusses arrays, strings, and related methods in Java. It covers: - How to create and define arrays, including single and multi-dimensional arrays. Arrays are fixed in size and hold homogeneous data. - The Object and String classes in Java and how to create String objects using literals or the new keyword. - The StringBuffer class, which is like String but mutable, allowing the string to be modified. - Other topics covered include default values in arrays, the StringTokenizer class, and differences between String and StringBuffer.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

J6-ARRAYS & String Handling

This document discusses arrays, strings, and related methods in Java. It covers: - How to create and define arrays, including single and multi-dimensional arrays. Arrays are fixed in size and hold homogeneous data. - The Object and String classes in Java and how to create String objects using literals or the new keyword. - The StringBuffer class, which is like String but mutable, allowing the string to be modified. - Other topics covered include default values in arrays, the StringTokenizer class, and differences between String and StringBuffer.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

ARRAYS AND STRING: WORKING WITH

ARRAYS, STRINGS AND ITS METHODS


Dr B.Veera Jyothi
Arrays and Strings
Working with Arrays , Strings and its methods

Dr B.Veera Jyothi
Assistant Professor
IT Department,CBIT.
ARRAYS: HOW TO CREATE AND DEFINE ARRAYS

 Introduction

 Array declaration

 Array construction

 Array initialization

 Array declaration, construction, 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 x=10; Fixed Size


int y=20; Homogeneous Data Type
int z=30;
int x[]=new int[1000];
ARRAYS: HOW TO CREATE AND DEFINE ARRAYS
 An array is an indexed collection of fixed number of
homogeneous data elements.
Advantage of arrays:
Advantage of arrays is we can represent multiple values with
the same name so that readability of the code will be improved.
Disadvantage of arrays is:
Disadvantage of arrays is Fixed in size that is once we
created an array there is no chance of increasing or decreasing the
size based on our requirement that is to use arrays concept
compulsory we should know the size in advance which may not
possible always.
We can resolve this problem by using collections.
ARRAYS: HOW TO CREATE AND DEFINE ARRAYS
 Array declarations:
Single dimensional array declaration:
Example:
int[] a; * //recommended to use
int []a;
int a[];

At the time of declaration we can't specify the size otherwise we


will get compile time error.
Example:

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[][]; //All are valid.(6 ways)

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);
}
}

A. 0 B. 1 C. 2 D. 3 E. 4


ARRAYS: HOW TO CREATE AND DEFINE ARRAYS
 Array construction:
 Every array in java is an object hence we can create by using new
operator.

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:

char[] ch={'a','e','i','o','u'}; //valid

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”));

StringBuffer s=new stringBuffer(“CBIT”);

s.append(“ECE”);

System.out.println(s);
JAVA STRING BUFFER CLASS

String s1=new string(“CBIT”);

String s2=new string(“CBIT”);

System.out.println(s1==s2);

System.out.println(s1.equals(s2));

StringBuffer sb1=new stringBuffer(“CBIT”);

StringBuffer sb2=new stringBuffer(“CBIT”);

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

You might also like