排除除 1,2,3 以外的数字的程序
program to exclude number having apart from 1,2,3
我只是在练习一些编码问题,我得到的问题定义类似于
Given an array of numbers, the task is to print only those numbers which have only 1, 2 and 3 as their digits.
为此我有这样写的代码
public class PrintArrays {
public static void main(String[] args) {
PrintArrays p = new PrintArrays();
List<Integer> list = p.findNumbers(new int[]{22,123,456,145,5,3,000,10,453});
String s = new String("23");
list.forEach(data -> System.out.println(data));
}
private List<Integer> findNumbers(int[] is) {
List<Integer> list = new ArrayList<>();
Arrays.stream(is)
.filter(data -> !String.valueOf(data)
.matches("(0|[a-zA-Z4-9].*)"))//tried to match if it contains alphabets or any other number apart from 1,2,3
.sorted()
.forEach(data -> list
.add(data));
return list;
}
}
我的期望输出是
3
22
123
我得到了什么:
3
10
22
123
145
请帮助我改进正则表达式
你可以直接替换:
.filter(data -> !String.valueOf(data).matches("(0|[a-zA-Z4-9].*)"))
和
.filter(data -> String.valueOf(data).matches("[123]+"))
我进行了两项更改,删除了非运算符 !
并使用了此正则表达式 [123]+
匹配 class [123]
中的一个或多个数字
输出
3
22
123
您不需要使用正则表达式来完成任务 - 一个简单的循环枚举 int
的数字就足够了:
static boolean is123(int x) {
if (x == 0) { // Treat zero as a special case
return false;
}
while (x != 0) { // Divide by ten until we get to zero
if (x%10 < 1 || x%10 > 3) {
// If we get a digit other than 1, 2, or 3, return false
return false;
}
x /= 10;
}
return true;
}
以下是如何使用它过滤数字:
List<Integer> result = Arrays.stream(is)
.filter(Test::is123) // Test is the name of the class enclosing is123 method
.sorted()
.boxed()
.collect(Collectors.toList());
我只是在练习一些编码问题,我得到的问题定义类似于
Given an array of numbers, the task is to print only those numbers which have only 1, 2 and 3 as their digits.
为此我有这样写的代码
public class PrintArrays {
public static void main(String[] args) {
PrintArrays p = new PrintArrays();
List<Integer> list = p.findNumbers(new int[]{22,123,456,145,5,3,000,10,453});
String s = new String("23");
list.forEach(data -> System.out.println(data));
}
private List<Integer> findNumbers(int[] is) {
List<Integer> list = new ArrayList<>();
Arrays.stream(is)
.filter(data -> !String.valueOf(data)
.matches("(0|[a-zA-Z4-9].*)"))//tried to match if it contains alphabets or any other number apart from 1,2,3
.sorted()
.forEach(data -> list
.add(data));
return list;
}
}
我的期望输出是
3
22
123
我得到了什么:
3
10
22
123
145
请帮助我改进正则表达式
你可以直接替换:
.filter(data -> !String.valueOf(data).matches("(0|[a-zA-Z4-9].*)"))
和
.filter(data -> String.valueOf(data).matches("[123]+"))
我进行了两项更改,删除了非运算符 !
并使用了此正则表达式 [123]+
匹配 class [123]
输出
3
22
123
您不需要使用正则表达式来完成任务 - 一个简单的循环枚举 int
的数字就足够了:
static boolean is123(int x) {
if (x == 0) { // Treat zero as a special case
return false;
}
while (x != 0) { // Divide by ten until we get to zero
if (x%10 < 1 || x%10 > 3) {
// If we get a digit other than 1, 2, or 3, return false
return false;
}
x /= 10;
}
return true;
}
以下是如何使用它过滤数字:
List<Integer> result = Arrays.stream(is)
.filter(Test::is123) // Test is the name of the class enclosing is123 method
.sorted()
.boxed()
.collect(Collectors.toList());