Unit 3-Arrays, Strings and Vectors
Unit 3-Arrays, Strings and Vectors
Example :
int[] number;
or
int number[];
Creating Arrays
You can create an array by using the new
operator with the following syntax:
arrayRefVar = new
dataType[arraySize];
e.g.
int number[]={2,3,4,5,6,7};
Example: To find sum of array elements
class TestArray
{
public static void main(String[] args)
{
int myList[] = {1, 2, 3, 3};
for (int i = 0; i < myList.length; i++)
{ length: It is a final
System.out.println(myList[i] + "variable
"); and only
applicable for array. It
represent size of array.
}
System.out.println("arraylength="+myList.
length);
// Summing all elements
int total = 0;
for (int i = 0; i < myList.length; i++)
{
total += myList[i];
}
System.out.println("Total is " + total);
}
}
Example: To find sum of array elements accepted by the
user
import java.util.Scanner;
class ArraySumDemo {
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int a[] = new int[10];
int sum = 0;
System.out.println("Enter the
elements:");
for (int i=0; i<10; i++)
{
a[i] = sc.nextInt();
}
System.out.println("arraylength="+array
.length);
for( int j=0;j<10;j++)
{
sum = sum+a[j];
}
System.out.println("Sum of array
elements is:"+sum);
}
}
Question
arr[][]={{1,2,3},{2,4,5},
{4,4,5}};
arr[][]={
{1,2,3},
{2,4,5},
{4,4,5}
};
Example class Testarray
{
public static void main(String args[])
{
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},
{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
Question
contd…
for ( i = 0 ; i < m ; i++ )
{
for ( j = 0 ; j < n ; j++ )
{
sum[i][j] = first[i][j] +
second[i][j];
}
}
contd…
System.out.println("Sum of entered
matrices:-");
for ( i = 0 ; i < m ; i++ )
{
for ( j = 0 ; j < n ; j++ )
{
System.out.print(sum[i][j]+"\t");
}
System.out.println();
}
}
}
System.arraycopy()
The java.lang.System.arraycopy() method
copies an array from the specified source
array, beginning at the specified position, to
the specified position of the destination
array.
A subsequence of array components are
copied from the source array referenced
by src to the destination array referenced
by dest.
The number of components copied is equal
to the length argument.
This method does not return any value.
System.arraycopy()
Syntax :
System.arraycopy(srcArray, srcPos, destArray,
destPos, length)
Where,
srcArray is Source Arrayname
srcPos index of source array from which array is to
be copied
destArray is Destination Arrayname
destPos is index of destination array where the
copied array is to put.
Length is the number of elements to be copied
Example
public class SystemArrayCopy
{
public static void main(String[] args)
{
int arr1[] = { 0, 1, 2, 3, 4, 5 };
int arr2[] = { 5, 10, 20, 30, 40, 50 };
// copies an array from the specified source
array
System.arraycopy(arr1, 3, arr2, 3, 3);
System.out.print("array2 = ");
for(int i=0;i<6;i++)
{ System.out.print(arr2[i] + " ");
}
}
}
Question
StringBuffer
e.g.
String firstName=new String(“Sachin”);
or
String s=“Sachin”;
Example
class StringExample
{
public static void main(String args[])
{
String s1="java"; //creating string by java string literal
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch); //converting char array to string
Example:
class StringArray
{
public static void main(String[] args)
{
String[] s1 = {“abc", “xyz", “pqr"};
int size = s1.length;
for (int i=0; i<size; i++)
{ System.out.println(s1[i]); }
}
}
String methods
Method call Task performed
s2=s1.tolowerCase; Convert the string to all lowercase
s2=s1.toupperCase; Convert the string to all uppercase
s2=s1.replace(‘x’,’y’) Replace all appearances of x with y
;
s2=s1.trim(); Remove white spaces at the beginning
and end of the string s1
s1.equals(s2); Return true if s1=s2
s1.equalsIgnoreCase Return true if s1=s2, ignoring the case
(s2) of characters
s1.length() Gives the length of s1
s1.CharAt(n) Gives n th character of s1
s1.compareTo(s2) Returns negative if s1<s2,positive if
s1>s2 and zero if s1=s2
s1.concat(s2) Concatenates s1 and s2
Contd…
S1.substring(n) Gives substring starting from n th
character
S1.substring(n,m) Gives substring starting from n th
character up to m th(excluding m th
character)
p.toString() Creates a string representation of
object p
S1.indexOf(‘x’) Gives the position of the first
occurrence of ‘x’ in the string s1
class String_method
{
public static void main(String args[])
{
String s="Sachin";
System.out.println(s.length());
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());
System.out.println(s); //Sachin (no change in
original)
System.out.println(s.charAt(3));
String ss=“ Virat Kohali ";
System.out.println(ss.trim());
System.out.println(ss.substring(3));
contd…
Contd…
System.out.println(ss.substring(2,6));
System.out.println(ss.indexOf('a'));
System.out.println(ss.indexOf('a',7));
int a=10;
String s2=String.valueOf(a);
System.out.println(s2+10);
String s1="Java is a programming language. Java is a platform.";
String replaceString=s1.replace(“Java",“Python");//replaces all
occurrences of
"Java" to " Python "
System.out.println(replaceString);
System.out.println("Hello".concat("World"));
System.out.println("Hello".compareTo("World"));
}
}
Ouput
6
SACHIN
sachin
Sachin
h
Virat Kohali
irat Kohali
Vira
5
11
1010
Python is a programming language. Python is
a platform.
HelloWorld
-15
Question 1
contd…
Contd…
if (a.contains(S1))
{
index1 = a.indexOf(S1);
}
if (a.contains(S2))
{
index2 = a.indexOf(S2);
}
if(index1<index2)
{
System.out.println(a.substring(index1+1,index
2));
}
}
}
StringBuffer Class
StringBuffer is a peer class of String.
While String creates strings of fixed length,
StringBuffer creates string of flexible length that
can be modified in terms of length and content.
We can insert characters and substrings in the
middle of a string or append another string to
the end.
Syntax :
Object nextElement( )
2 This returns the next object in the enumeration as a
generic Object reference.
Wrapper Classes