I have an array of String containing below elements
5.0,99,5.5,100,6.0,101
Now what I want to do I need to remove all the decimal value in string format like 5.0,5.5
and 6.0
so my final array should contain elements 99,100,101
so what I'd done so far is show in below code
public static String[] deleteElement(String[] str_array) {
//String[] str_array = {"item1","item2","item3"};
List<String> list = new ArrayList<String>(Arrays.asList(str_array));
list.remove("5.0");
list.remove("5.5");
list.remove("6.0");
return str_array = list.toArray(new String[0]);
}
I have hardcoded the values which is quite bad practice as the values in future may be more than the current values, so I want a way to remove by using index
.
I have tried to remove using remove(index)
also, using even index positioning like 0,2,4,but what actually happening is, after deletion of 5.0
, elements are shifted left side, so 100 comes into the 2nd position, and in next iteration 100 deleted.
So is there any way where I can construct a generic function so if there are more elements in decimal value so it can also be deleted.
NOTE : The string decimal values will always be residing on even index positioning.
You can go with the below approach, it worked fine for me !!
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DecimalRemoval {
public static void main(String[] args) {
List<String> lst = Arrays.asList("5.0","99","5.5","100","6.0","101");
List<String> resultant = new ArrayList<>(lst.size());
for(String element: lst) {
if(! element.contains("."))
resultant.add(element);
}
System.out.println(resultant);
}
}
Also attaching the screenshot from my workspace.
We have traversed the List
from list.size() -1
to 0
So that number arent pushed in front.
when you traverse it from 0
to size of the list
and then if you delete an element 1
element is skipped cause size()
of the list decreases and i
keeps incrementing.
eg.["5.0","6.1","5.5","100","6.0","6.6"] when you traverse form 0
to size() - 1
i = 0:
5.0
is seen and removed and then the list is like ["6.1","5.5","100","6.0","6.6"]
where now 6.1
is at 0
index.
i = 1:
5.5
is removed and then the list is like ["6.1","100","6.0","6.6"]
where now 100
is at 1
index.
and so on.
But when traverse from size-1
to 0
it will remove every single element without the fear of missing any decimal
number.
Try this on your own and you will get to know why it is working and why 0
to n-1
isn't working.
String arr[] = {"5.0","99","5.5","100","6.0","101"};
List<String> list = new ArrayList<String>(Arrays.asList(arr));
for(int i = list.size() - 1 ; i >= 0 ; i--) {
String number = list.get(i);
if(number.contains(".")) {
list.remove(number);
}
}
for(String val : list) {
System.out.println(val);
}
output:
99
100
101
The existing answers work but are more verbose than required:
list.removeIf(str -> str.contains("."));
You can also just stream the array and filter out the unwanted values.
String[] v = {"5.0","99","5.5","100","6.0","101"};
v = Arrays.stream(v)
.filter(str->!str.contains("."))
.toArray(String[]::new);
System.out.println(Arrays.toString(v));
Prints
[99, 100, 101]
A simple solution can be:
ArrayList<String>
..
Demo:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static String[] deleteElement(String[] str_array) {
List<String> list = new ArrayList<>();
for (String s : str_array) {
if (!s.contains(".")) {
list.add(s);
}
}
return list.toArray(new String[0]);
}
public static void main(String[] args) {
// Test
System.out.println(Arrays.toString(deleteElement(new String[] { "5.0", "99", "5.5", "100", "6.0", "101" })));
}
}
Output:
[99, 100, 101]
Alternatively, you can do it in the way you are already doing but it is not so efficient way. Since you are first adding all the elements to the list and then removing the unwanted ones, therefore it is not so efficient as the one suggested above.
public static String[] deleteElement(String[] str_array) {
List<String> list = new ArrayList<String>(Arrays.asList(str_array));
for (String s : str_array) {
if (s.contains(".")) {
list.remove(s);
}
}
return list.toArray(new String[0]);
}
For Java 8 users, Best solution would be using the Java lambda expressions .
list.removeIf(element -> element.contains("."));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.