-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathremove.java
More file actions
34 lines (27 loc) · 780 Bytes
/
remove.java
File metadata and controls
34 lines (27 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Java Program to remove a
// given object from the array
import java.util.*;
public class Main {
public static void main(String args[])
{
// Given an array of String objects
String[] arr
= { "Geeks", "for", "Geeks", "hello", "world" };
// object to be removed
String removeObj = "Geeks";
// Converting the array to list
List<String> list
= new ArrayList<String>(Arrays.asList(arr));
// Remove all occurrences of given string
list.removeAll(Arrays.asList(removeObj));
// Convert back list to array
// the length of array
// will also be updated
arr = list.toArray(new String[0]);
// Print the updated array
System.out.println("Updated array:- ");
for (int ind = 0; ind < arr.length; ind++) {
System.out.println(arr[ind]);
}
}
}