Dt : 18/2/2022
faq:
define Extended for loop?
=>Extended for loop introduced by Java5 version and which is
Auto-retrieval loop used to display elements from Array objects.
syntax:
ii
for(data_type var_name : Container_name)
ath
{
//loop_body
aip
}
Ex:
hM
for(Integer k : a)
//loop_body
tes
}
a
Advantage:
nk
=>In Extended for we use Container_name and data_type,but
there is no concept of Initialization,Condition and Incre/Decre.
Ve
Note:
=>This Extended for also known as Enhanced for loop or for-each
loop.
========================================================
Ex_program :
Wap to read and display String objects using Array?
DemoArray2.java
package maccess;
import java.util.*;
public class DemoArray2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
try(s;){
ii
try {
ath
System.out.println
("Enter the Number of String Objects:");
int n = Integer.parseInt(s.nextLine());
//read in the form of String and Convert into
integer
aip
String a[] = new String[n];
//Array to hold n String objects
System.out.println
("Enter "+n+" String Objects");
hM
for(int i=0;i<a.length;i++)
{
a[i] = s.nextLine();
}//end of loop
System.out.println
("===Display using Old for loop==");
tes
for(int i=0;i<a.length;i++)
{
System.out.print(a[i].toString()+" ");
}//end of loop
a
System.out.println
nk
("\n===Display using Entended for===");
for(String k : a)
{
System.out.print(k.toString()+" ");
Ve
}//end of loop
}catch(Exception e) {
e.printStackTrace();
}
}//try with resource
}
}
o/p:
Enter the Number of String Objects:
Enter 3 String Objects
java
program
ii
nit
ath
===Display using Old for loop==
java program nit
aip
===Display using Entended for===
java program nit
hM
========================================================
Ex_Program :
Wap to read and display User defined class Objects?
tes
Product.java
package test;
public class Product {
a
public String pCode,pName;
public float pPrice;
nk
public int pQty;
public Product(String pCode,String pName,float pPrice,
int pQty) {
Ve
this.pCode=pCode;
this.pName=pName;
this.pPrice=pPrice;
this.pQty=pQty;
}
public String toString() {
return pCode+"\t"+pName+"\t"+pPrice+"\t"+pQty;
}
}
DemoArray3.java
package maccess;
import java.util.*;
import test.Product;
public class DemoArray3 {
public static void main(String[] args) {
ii
Scanner s = new Scanner(System.in);
ath
try(s;){
try {
aip
System.out.println
("Enter the number of Products:");
hM
int n = Integer.parseInt(s.nextLine());
Product p[] = new Product[n];
//Array holding n product objects
tes
System.out.println("Enter "+n+" Products:");
for(int i=0;i<p.length;i++)
a
{
nk
System.out.println("Enter the PCode:");
String pCode = s.nextLine();
Ve
System.out.println("Enter the PName:");
String pName = s.nextLine();
System.out.println("Enter the PPrice:");
float pPrice =
Float.parseFloat(s.nextLine());
System.out.println("Enter the PQty:");
int pQty = Integer.parseInt(s.nextLine());
p[i] = new Product(pCode,pName,pPrice,pQty);
}//end of loop
System.out.println("==Uisng Old for loop==");
for(int i=0;i<p.length;i++)
ii
{
ath
System.out.println(p[i].toString());
}//end of loop
aip
System.out.println("===Using Extended for===");
for(Product k : p)
hM
{
System.out.println(k.toString());
}//end of loop
tes
}catch(Exception e) {e.printStackTrace();}
}//end of try-with-resource
a
}
nk
o/p:
Ve
Enter the number of Products:
Enter 3 Products:
Enter the PCode:
A121
Enter the PName:
Mou
Enter the PPrice:
123.45
Enter the PQty:
12
ii
Enter the PCode:
ath
A122
Enter the PName:
aip
KKB
Enter the PPrice:
hM
234.56
Enter the PQty:
10
tes
Enter the PCode:
A124
a
Enter the PName:
nk
CDR
Enter the PPrice:
Ve
456.34
Enter the PQty:
11
==Uisng Old for loop==
A121 Mou 123.45 12
A122 KKB 234.56 10
A124 CDR 456.34 11
===Using Extended for===
A121 Mou 123.45 12
A122 KKB 234.56 10
A124 CDR 456.34 11
ii
----------------------------------------------------
ath
Diagram:
aip
hM
tes
======================================================
a
Assignment:
nk
wap read and display Multiple Student details with result using
Ve
Array?
====================================================
faq:
define Object Array?
=>The Array which is declared with 'java.lang.Object' class is
known as Object Array.
syntax:
Object o[] = new Object[size];
Note:
=>'java.lang.Object' class is the PClass of all the classes used
ii
in JavaApp.
ath
=>Object Array can hold Dis-Similer objects,which means objects
of different classes.
Ex_program:
aip
hM
DemoArray4.java
package maccess;
import test.Product;
public class DemoArray4 {
@SuppressWarnings("removal")
tes
public static void main(String[] args) {
Object o[] = new Object[3];
//Object Array to hold 3 Objects
o[0] = new Integer(121);//Integer Object
a
o[1] = new String("XYZ");//String Object
o[2] = new Product("A121","KB",123,12);
nk
System.out.println("===Display from Object Array==");
for(Object k : o)
{
Ve
System.out.println(k.toString());
}//end of loop
}
}
o/p:
===Display from Object Array==
121
XYZ
A121 KB 123.0 12
==================================================
2.Multi Dimensional Arrays:
=>The Arrays which are declared with multiple dimensions are
ii
known as Multi-Dimensional Arrays.
ath
Ex:
2-D Arrays
aip
3-D Arrays
4-D Arrays
hM
.....
Note:
tes
=>In realtime Multi-D Arrays are less used when compared to
Single-D Arrays.
a
=>But,Using 2-D Arrays we can construct 'Jagged Arrays'.
nk
syntax of 2-D Array:
Class_name arr_var[][] = new Class_name[rows][cols];
Ve
faq:
define Jagged Arrays?
=>Array which is holding Array Objects is known as Jagged Array.
Ex_Program : DemoArray5.java
package maccess;
public class DemoArray5 {
public static void main(String[] args) {
Integer a1[] = {1,2,3};
Integer a2[] = {11,12,13,14};
Integer a[][] = {a1,a2};//Jagged_Array
System.out.println("===Display from Jagged_Array===");
for(Integer k[] : a)
{
System.out.print("Array : ");
ii
for(Integer z : k)
ath
{
System.out.print(z.toString()+" ");
}//InnerLoop or Nested loop
System.out.println();
}//end of outer loop
aip
}
}
hM
o/p:
===Display from Jagged_Array===
tes
Array : 1 2 3
Array : 11 12 13 14
a
nk
diagram:
Ve
ii
ath
aip
hM
=========================================================
a tes
nk
Ve