#33 Enhanced for Loop in Java

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

For More Queries WhatsApp or Call on : +919008963671

In this lecture we are discussing:
1)why do we need enhanced for loop if we have many different loop.
2)enhanced for loop use to iterate values of arrays and collection

#1
why do we need enhanced for loop ?
-- As of Java 5, the enhanced for loop was introduced. This is mainly used to traverse a collection of elements including arrays.
-- for traversing any collection and arrays without index we can use enhanced for loop.
-- syntax does not need increment statement, condition check statement .this can automatically iterate the array or collection without need of index.

#3
-- enhanced for loop can be used in array and collection to iterate the object
syntax
int nums[]=new int[4];
for(int n:nums){
}

for curiosity:
e.g use of enhanced for loop in collection
ArrayList al=new ArrayList();
for(Object o:al){
}

Note: Do not confuse yourself with collection in upcoming lecture we will discussing the collection.

More Learning :

Donation:
PayPal Id : navinreddy20
Рекомендации по теме
Комментарии
Автор

the best part of your teaching is that videos are short and covers everything so one doesn't get bored

GG-hkiz
Автор

Just a straight to the point video, I liked it!

kingtyphoon
Автор

Please make a video on problem solving in java

royalgamingmdk
Автор

Thanks for explaining with Arrays of Student type too. 👍🏻

Faraz-cse
Автор

This is my first comment on YouTube
Just I wanna say ....
Thank you sir

rifikagarigipati
Автор

Beautiful.. simple and concise... just brilliant

christyaldridge
Автор

THANK YOU SO MUCH sir....for helping me out with the coding.Grateful for you ever.

RagnarLothbrok-sqfj
Автор

Similar to the long time basic for loop in Python. ( and I gather other advanced languages )

russhensel
Автор

Total Array including Enhanced for loop concept :-

public class Array {
public static void main(String[] args) {
int num1[] = new int[3];
int num[][] = new int[3][4];
for (int i = 0; i < 3; i++) {
num1[i] = (int) (Math.random() * 10);
for (int j = 0; j < 4; j++) {
num[i][j] = (int) (Math.random() * 10);
}
}
System.out.println("\nTwo dimensitonal Array");

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(num[i][j] + " ");
}
System.out.println();
}
System.out.println("One dimensitonal Array");
for (int n : num1) {
System.out.print(n + " ");
}
System.out.println("\nTwo dimensitonal Array");
for (int n[] : num) {
for (int m : n)
System.out.print(m + " ");
System.out.println();
}
int num2[][] = new int[4][];// jagged array
num2[0] = new int[2];
num2[1] = new int[3];
num2[2] = new int[4];
num2[3] = new int[5];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < num2[i].length; j++) {
num2[i][j] = (int) (Math.random() * 10);
}
}
System.out.println("Jagged Array");
for (int n2[] : num2) {
for (int m2 : n2) {
System.out.print(m2 + " ");
}
System.out.println();
}
System.out.println("3D Array");
int num3[][][] = new int[3][2][2];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
num3[i][j][k] = (int) (Math.random() * 10);

}
}
}
System.out.println();
for (int n3[][] : num3) {
for (int n31[] : n3) {
for (int n311 : n31)
System.out.print(n311 + " ");
}
System.out.println();
}
System.out.println("Object Array(String)");

Friend f1 = new Friend();
Friend f2 = new Friend();
Friend f3 = new Friend();
f1.marks = 80;
f1.name = "Ramesh";
f2.marks = 90;
f2.name = "Suresh";
f3.marks = 85;
f3.name = "Avi";
Friend f[] = { f1, f2, f3 };
for (int i = 0; i < f.length; i++) {
System.out.println(f[i].name + " | " + f[i].marks);
}
System.out.println("Using enhanced for loop");
for (Friend friend : f) {
+ " | " + friend.marks);
}

}
}

class Friend {
int marks;
String name;
}

RameshMaity
Автор

Fist, great video and explanation! Thank you so much for all this.
I'd like to ask you and guys in the comment about how do you uncomment all those section by simply selecting them and press a button! Im very curious about this handy shortcut! Im a windows user maybe thats why I find it such a cool feature, maybe only for mac users ahahah.
Again thx for the video, got it all clear :D

Cladxz
Автор

why you given student in place of string or int in enhance for loop can u explain for me pls sir

raviteja-uu
Автор

Hi.. I tried with String array and does not work. Please let me know whether will it work with String array. Also how to use in enhanced for loop to print the String array.

nandhinim
Автор

How to print one student name and mark using enhanced for loop instead of print all student details?

praveenasubramaniyan
Автор

Why are you not updating VS Code? Please update VS Code.

puruagni
Автор

Whats the trick to comment a block of code easily?

MOHDYUSUF-lr
Автор

public class Main{
public static void main(String[] args) {

Student stud = new Student();
stud.roll = 1;
stud.marks = 88;
stud.name = "Kevin";

Student stud2 = new Student();
stud2.roll = 2;
stud2.marks = 78;
stud2.name = "Marco";

Student stud3 = new Student();
stud3.roll = 3;
stud3.marks = 90;
stud3.name = "Justin";

Student[] students = new Student[3];//Array with references
students[0] = stud;//Manual objects
students[1] = stud2;
students[2] = stud3;

for (Student student : students) {
System.out.print(student.name + " " + student.roll + " " + student.marks + "\n");
}

}
}

class Student {
int roll;
int marks;
String name;
}

kvelez
welcome to shbcf.ru