CREATIVE MULTIMEDIA INSTITUTE
CH – 03
Array & String in
JAVA
Page | 1
CREATIVE MULTIMEDIA INSTITUTE
Array :
An array is a group of variables of same data type which have same name.
Therefore it is used when more than one variables of same data type are to be used in a
program.
There are different types of array can be classified in JAVA.
(1) One dimensional Array
(2) Two dimensional Array
(3) Multi-dimensional Array
(4) Jagged Array
One dimensional Array :
A one dimensional array is a collection of variables with one row and multiple columns or
one column and multiple rows.
Declaration of one dimensional array:
Advantages :
Code Optimization: It makes the code optimized, we can retrieve or sort the data
efficiently.
Random access: We can get any data located at an index position.
Disadvantages :
Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its
size at runtime. To solve this problem, collection framework is used in Java which grows
automatically.
Page | 2
CREATIVE MULTIMEDIA INSTITUTE
Syntax:
Data_Type array_name[ ];
Example :
int sub[ ];
Here the sub is said to be array of integers. The array is just created
but it is not ready to use. To use it allocate memory space to it using
new keyword.
Syntax:
Array_name = new Data_Type[ ];
Example :
marks = new int [10];
Now marks is allocated size of 10 int values and it can be used now.
Alternatively we can combine these two syntax like:
int marks[ ] = new int[10];
Now to initialize array element you can refer to it by its index which starts from 0.
Def__ WAP to assign 5 values to an array. (Define individual values)
class sp
{ public static void main(String args[])
{ int a[];
a=new int[5];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
System.out.println("Value of a[0]="+a[0]);
System.out.println("Value of a[1]="+a[1]);
System.out.println("Value of a[2]="+a[2]);
System.out.println("Value of a[3]="+a[3]);
System.out.println("Value of a[4]="+a[4]);
}
}
Def__ WAP to assign 5 values to an array. (Define all values at a time)
Page | 3
CREATIVE MULTIMEDIA INSTITUTE
class sp
{ public static void main(String args[])
{ int a[ ]={10,20,30,40,50};
System.out.println("Value of a[0]="+a[0]);
System.out.println("Value of a[1]="+a[1]);
System.out.println("Value of a[2]="+a[2]);
System.out.println("Value of a[3]="+a[3]);
System.out.println("Value of a[4]="+a[4]);
}
}
Array with loop Example:
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
int i,n,ar[];
n=5;
ar=new int[n];
for(i=0;i<ar.length;i++)
{
System.out.print("Enter a["+i+"]=");
ar[i]=x.nextInt();
}
for(i=0;i<ar.length;i++)
{
System.out.println("A["+i+"]="+ar[i]);
}
}
For-each Loop for Java Array :
We can also print the Java array using for-each loop.
The Java for-each loop prints the array elements one by one. It holds an array element in
a variable, then executes the body of the loop.
Syntax :
for(data_type variable:array){
//body of the loop
}
Page | 4
CREATIVE MULTIMEDIA INSTITUTE
Example :
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
int i,n,ar[];
n=5;
ar=new int[n];
for(i=0;i<ar.length;i++){
System.out.print("Enter a["+i+"]=");
ar[i]=x.nextInt();
}
for(int a : ar)
{
System.out.println(a);
}
}
Two or Multi Dimensional Array :
The multi dimensional arrays are array of array. It represents a variable which has values
in tabular form i.e. in rows and columns.
A list of items can be store in table format by row and column is known as Two-
Dimensional Array.
How to declare two dimensional array?
Syntax :
<datatype> NameOfArray[][]
= new
<datatype>[int][int];
Purpose :
To declare two dimensional array we can use
above syntax.
Example :
int twoD[][] = new int[4][5];
Page | 5
CREATIVE MULTIMEDIA INSTITUTE
Def.__ WAP to store result of 5 students with 4 subjects using TWO Dimensional array.
Then print total, percent using loop.
class TwoDArray
{ public static void main(String args[])
{ int x, y;
int sub[][]={
{50,50,46,43}, {60,40,47,44},
{70,40,48,45}, {80,45,49,46},
{90,55,49,47} };
for(x=0; x<5; x++)
{System.out.print("\nStudnet "+(x+1)+ ": ");
for(y=0; y<4; y++)
{ System.out.print(sub[x][y]);
System.out.print(",");
}
}
}
}
Jagged Array :
It is an array of arrays where each
element is, in turn, an array. A special
feature of this type of array is that it is a
Multidimensional array whose each
element can have different sizes.
Syntax:
Data_type array_name[ ][ ]=new data_type[Rowsize][ ];
Purpose :
To declare a jagged array we can use above syntax.
Example :
double d[ ][ ] = new double[5][ ];
int j[ ][ ]=new int[5][ ];
Page | 6
CREATIVE MULTIMEDIA INSTITUTE
Here j is a jagged array having 5 rows.
Now we can declare the size of each columns for each row as shown below.
j[0]=new int[3];
j[1]=new int[2];
j[2]=new int[4];
j[3]=new int[3];
Def. __ Demonstrate use of jagged array.
int i,n,ar[][];
n=5;
ar=new int[3][];
ar[0]=new int[2];
ar[1]=new int[3];
ar[2]=new int[2];
for(i=0;i<3;i++){
for(int j=0;j<ar[i].length;j++)
{
ar[i][j]=j;
System.out.print(ar[i][j]);
}
System.out.println("");
}
sss
Strings in Java are Objects that are backed internally by a char array. Since arrays are
immutable(cannot grow), Strings are immutable as well. Whenever a change to a
String is made, an entirely new String is created.
1. String s1="Creative"; //creating string by java string literal
2. String s2=new String(ch); //converting char array to string
3. String s3=new String("example"); //creating java string by new keyword
Example :
Page | 7
CREATIVE MULTIMEDIA INSTITUTE
char[] ch={‘c’,’r’,’e’,’a’,’t’,’I’,’v’,’e’};
String s=new String(ch);
System.out.print(s);
Java String class methods
The java.lang.String class provides many useful methods to perform operations on
sequence of char values.
No. Method Description
1 char charAt(int index) returns char value for the particular index
2 int length() returns string length
5 String substring(int beginIndex) returns substring for given begin index
7 boolean contains(CharSequence s) returns true or false after matching the sequence of char value
13 String replace(char old, char new) replaces all occurrences of specified char value
16 String split(String regex) returns splitted string matching regex
19 int indexOf(int ch) returns specified char value index
23 String toLowerCase() returns string in lowercase.
25 String toUpperCase() returns string in uppercase.
Java Command Line Arguments :
The java command-line argument is an argument i.e. passed at the time of running
the java program.
The arguments passed from the console can be received in the java program and it
can be used as an input.
So, it provides a convenient way to check the behavior of the program for the
different values. You can pass N (1,2,3 and so on) numbers of arguments from the
command prompt.
Example :
class A{
public static void main(String args[]){
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}
Page | 8