[英]Java: I=How to use ArrayList.contain() within a single element
So I have some java code that needs to throw out every single element containing the number 5 and return the count.所以我有一些 java 代码需要丢弃包含数字 5 的每个元素并返回计数。 So far I have:
到目前为止,我有:
public static int noFive(int start, int end) {
ArrayList<Integer> arr = new ArrayList<Integer>();
int result = end - start + 1;
for (int i = start; i <= end; i++) {
arr.add(i);
}
for (Integer i : arr) {
if (i % 5 == 0) { //my problem is here i try to do this but i know this does not eliminate numbers such as 51, 52, 53 etc.
result--;
} else {
continue;
}
}
System.out.println(result);
return result;
}
I searched up the ArrayList doc and it says that the contains method only throws out the elements containing 5 and only 5. But I want a program that throws out stuff like 51, 52, 53, stuff that are not divisible by 5.我搜索了 ArrayList 文档,它说 contains 方法只抛出包含 5 且只有 5 的元素。但我想要一个程序抛出诸如 51、52、53 之类的东西,这些东西不能被 5 整除。
A workaround is convert each Integer to String and use contains()一种解决方法是将每个 Integer 转换为 String 并使用 contains()
Try this.尝试这个。 I just modified the @stackguy code.
我刚刚修改了@stackguy 代码。 ideone link https://ideone.com/aL7EFt
ideone链接https://ideone.com/aL7EFt
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception{
noFive(1,60);
}
public static int noFive(int start, int end) {
ArrayList<Integer> arr = new ArrayList<Integer>();
int result = end - start + 1;
for (int i = start; i <= end; i++) {
arr.add(i);
}
for (Integer i : arr) {
if (!(i % 5 == 0) && hasFive(i)) { //it will remove all numbers as 51, 52, 53 etc.
result--;
System.out.print(i+",");
}
}
System.out.println(result);
return result;
}
private static boolean hasFive(int num) {
int rem;
while (num > 0) {
rem = num % 10;
if (rem == 5)
return true;
num = num / 10;
}
return false;
}
}
It's important to realize that if you ask whether an integer contains 5, you actually view the integer as a series of digits, rather than a arithmetic value.重要的是要认识到,如果您询问 integer 是否包含5,您实际上将 integer 视为一系列数字,而不是算术值。
So what we could do is converting the number to a String and then check if the digit is present.所以我们可以做的是将数字转换为字符串,然后检查数字是否存在。 So we're only adding the number to the list if it does not contain the character
5
.因此,如果它不包含字符
5
,我们只会将数字添加到列表中。
List<Integer> list = new ArrayList<>();
for (int n = start; n <= end; n++) {
if (String.valueOf(n).indexOf('5') == -1) {
list.add(Integer.valueOf(n));
}
}
Or using streams:或使用流:
IntStream.rangeClosed(start, end)
.filter(n -> String.valueOf(n).indexOf('5') == -1)
.mapToObj(i -> i)
.collect(Collectors.toList());
Did you try something like this:你有没有尝试过这样的事情:
public static void main(String[] args) {
noFiveCount(1, 9); //prints 8
noFiveCount(1, 49); //prints 44
noFiveCount(1, 59); //prints 44
noFiveCount(1, 60); //prints 45
}
private static int noFiveCount(int start, int end){
int result = end - start + 1;
List<Integer> range = IntStream.rangeClosed(start, end)
.boxed().collect(Collectors.toList());
for (Integer i : range) {
if (hasFive(i)) {
result--;
} else {
continue;
}
}
System.out.println(result);
return result;
}
private static boolean hasFive(int num) {
int rem;
while (num > 0) {
rem = num % 10;
if (rem == 5)
return true;
num = num / 10;
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.