🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In Java, for loops are fundamental control structures used to iterate over a range of values. Sometimes, it's necessary to know when you're on the last iteration of a for loop, perhaps to handle a special case or to avoid adding a separator in a string builder scenario.
While Java's for loop doesn't provide a built-in way to identify the last iteration, there are several techniques you can use to detect it. In this blog post, we'll explore how to find the last iteration of a for loop in Java.
Method 1: Using a Counter Variable
One common method is to use a counter variable and compare it with the total number of iterations.
Example:
public class ForLoopLastIteration {
public static void main(String[] args) {
String[] items = {"apple", "banana", "cherry", "date"};
int totalItems = items.length;
for (int i = 0; i < totalItems; i++) {
System.out.print(items[i]);
if (i < totalItems - 1) {
System.out.print(", ");
} else {
System.out.println(" - Last Item!");
}
}
}
}
Output:
apple, banana, cherry, date - Last Item!
Method 2: Using a Flag Variable
Example:
public class ForLoopLastIteration {
public static void main(String[] args) {
String[] items = {"apple", "banana", "cherry", "date"};
boolean isLastIteration;
for (int i = 0; i < items.length; i++) {
isLastIteration = (i == items.length - 1);
System.out.print(items[i]);
if (!isLastIteration) {
System.out.print(", ");
} else {
System.out.println(" - Last Item!");
}
}
}
}
Output:
apple, banana, cherry, date - Last Item!
Method 3: Using the Enhanced For Loop
Example:
public class ForLoopLastIteration {
public static void main(String[] args) {
String[] items = {"apple", "banana", "cherry", "date"};
int counter = 0;
int totalItems = items.length;
for (String item : items) {
System.out.print(item);
if (counter < totalItems - 1) {
System.out.print(", ");
} else {
System.out.println(" - Last Item!");
}
counter++;
}
}
}
Output:
apple, banana, cherry, date - Last Item!
Conclusion
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment