🎓 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
System.arraycopy method, and converting the array to a list and back to an array.Table of Contents
- Introduction
- Using Loops
- Using
System.arraycopyMethod - Using
ArrayList - Conclusion
Introduction
In Java, arrays are fixed-size data structures that store elements of the same type. Removing an element from an array involves creating a new array without the unwanted element. This can be done using various methods, each suited to different scenarios.
Using Loops
One way to remove an element from an array is by iterating through the array and copying elements to a new array, skipping the element to be removed.
Example
import java.util.Arrays;
public class RemoveElementExample {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int elementToRemove = 3;
int[] result = removeElement(array, elementToRemove);
System.out.println("Original Array: " + Arrays.toString(array));
System.out.println("Array after removing " + elementToRemove + ": " + Arrays.toString(result));
}
public static int[] removeElement(int[] array, int element) {
int count = 0;
for (int i : array) {
if (i != element) {
count++;
}
}
int[] newArray = new int[count];
int index = 0;
for (int i : array) {
if (i != element) {
newArray[index++] = i;
}
}
return newArray;
}
}
Explanation
- The
removeElementmethod iterates through the original array to count the elements that are not equal to the element to be removed. - A new array is created with the size equal to the count of elements that are not to be removed.
- The original array is iterated again, and elements that are not equal to the element to be removed are copied to the new array.
Output:
Original Array: [1, 2, 3, 4, 5]
Array after removing 3: [1, 2, 4, 5]
Using System.arraycopy Method
The System.arraycopy method can be used to copy parts of the original array to a new array, excluding the element to be removed.
Example
import java.util.Arrays;
public class RemoveElementExample {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int elementToRemove = 3;
int[] result = removeElement(array, elementToRemove);
System.out.println("Original Array: " + Arrays.toString(array));
System.out.println("Array after removing " + elementToRemove + ": " + Arrays.toString(result));
}
public static int[] removeElement(int[] array, int element) {
int index = -1;
for (int i = 0; i < array.length; i++) {
if (array[i] == element) {
index = i;
break;
}
}
if (index == -1) {
return array; // Element not found
}
int[] newArray = new int[array.length - 1];
System.arraycopy(array, 0, newArray, 0, index);
System.arraycopy(array, index + 1, newArray, index, array.length - index - 1);
return newArray;
}
}
Explanation
- The
removeElementmethod finds the index of the element to be removed. - If the element is not found, the original array is returned.
- Two calls to
System.arraycopyare used to copy the parts of the array before and after the element to be removed into a new array.
Output:
Original Array: [1, 2, 3, 4, 5]
Array after removing 3: [1, 2, 4, 5]
Using ArrayList
Another way to remove an element from an array is by converting the array to an ArrayList, removing the element, and converting it back to an array.
Example
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class RemoveElementExample {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int elementToRemove = 3;
int[] result = removeElement(array, elementToRemove);
System.out.println("Original Array: " + Arrays.toString(array));
System.out.println("Array after removing " + elementToRemove + ": " + Arrays.toString(result));
}
public static int[] removeElement(int[] array, int element) {
List<Integer> list = new ArrayList<>();
for (int i : array) {
if (i != element) {
list.add(i);
}
}
int[] newArray = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
newArray[i] = list.get(i);
}
return newArray;
}
}
Explanation
- The
removeElementmethod creates anArrayListand adds all elements from the array that are not equal to the element to be removed. - A new array is created with the size of the
ArrayList. - The elements from the
ArrayListare copied back to the new array.
Output:
Original Array: [1, 2, 3, 4, 5]
Array after removing 3: [1, 2, 4, 5]
Conclusion
Removing an element from an array in Java can be accomplished using various methods, each with its own advantages. Using loops provides a clear and straightforward approach, suitable for any type of array. The System.arraycopy method offers a more efficient way to copy parts of the array. Using an ArrayList simplifies the process by leveraging dynamic resizing. Depending on your specific use case and preferences, you can choose the method that best fits your needs.
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