Java 如果某个参数属于某种类型,如何跳过该参数
因此,我试图找出如何跳过包含特定类型的参数,例如: 参数:蓝色3红色7绿色5黄色2 我想将数字存储在一个数组中,将颜色存储在一个单独的数组中。因此,如果args[i]是字符串,则将其存储在颜色数组中,如果是int,则将其存储在数字数组中。像ifargs[i]==String之类的东西,然后依此类推。显然,这不起作用,所以我在寻找另一个解决方案Java 如果某个参数属于某种类型,如何跳过该参数,java,Java,因此,我试图找出如何跳过包含特定类型的参数,例如: 参数:蓝色3红色7绿色5黄色2 我想将数字存储在一个数组中,将颜色存储在一个单独的数组中。因此,如果args[i]是字符串,则将其存储在颜色数组中,如果是int,则将其存储在数字数组中。像ifargs[i]==String之类的东西,然后依此类推。显然,这不起作用,所以我在寻找另一个解决方案 public class Main { public static void main(String[] args) {
public class Main {
public static void main(String[] args)
{
String[] colors = new String[] {};
int[] number = new int[] {};
for(int i = 0; i < args.length; i++)
{
// stuck
}
}
提前感谢您的帮助。试试这个
public static void main(String[] args) {
// args = Blue 3 Red 7 Green 5 Yellow 2
String[] colors = Arrays.stream(args).filter(str -> str.matches("\\D+")).toArray(String[]::new);
int[] number = Arrays.stream(args).filter(str -> str.matches("[-+]?\\d+")).mapToInt(Integer::parseInt).toArray();
System.out.println(Arrays.toString(colors)); // [Blue, Red, Green, Yellow]
System.out.println(Arrays.toString(number)); // [3, 7, 5, 2]
}
public class Main {
public static void main(String[] args)
{
String[] colors = new String[args.length] ;
int color_ix=0;
int number_idx=0;
Integer[] number = new Integer[args.length] ;
for(int i = 0; i < args.length; i++)
{
if(args[i]==null) {continue;}
try
{
number[number_idx]=Integer.parseInt(args[i]);
number_idx++;
}
catch(NumberFormatException e)
{
colors[color_ix]=args[i];
color_ix++;
}
}
System.out.println("-----Number-----");
for(int i=0;i<number_idx;i++)
{
System.out.println(number[i]);
}
System.out.println("-----Colors-----");
for(int i=0;i<color_ix;i++)
{
System.out.println(colors[i]);
}
}
}
试试这个:
public class Main {
public static void main(String[] args) {
List<String> colors = new ArrayList<>();
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < args.length; i++) {
try {
numbers.add(Integer.parseInt(args[i]));
} catch (NumberFormatException e) {
colors.add(args[i]);
}
}
String[] colorsArray = colors.toArray(new String[0]);
int[] number = numbers.stream().mapToInt(num -> num).toArray();
}
}
你可以简单地
一,。为数字和字符串创建ArrayList
二,。根据检查参数是数字还是字符串的正则表达式匹配向列表中添加值
三,。将ArrayList转换为数组
您可以使用ApacheCommonsLang检查它是否为数字
//initialized with max length
String[] colors = new String[args.length];
int[] number = new int[args.length];
int colorIndex=-1;
int numberIndex=-1;
for(int i = 0; i < args.length; i++)
{
//if number add to number array else to colors
if(StringUtils.isNumeric(args[i])){
number[++numberIndex]=Integer.valueOf(args[i]);
}else{
colors[++colorIndex]=args[i];
}
}
您可能正在查找instanceof,但由于args是字符串[],因此其所有元素都将是字符串或null。instanceof通常是一个线索,但并不总是一个线索,表明你的抽象没有完全正确;您可以使用do-a-thing方法定义一个接口,以及该do-a-thing的多个实现(使用字符串)、do-a-thing和int等。所有参数都是字符串,请尝试转换它们以确定字符串是否仅包含数字,请使用[static]类java.lang.Integer的方法parseIntString。我认为这个问题会对您有所帮助,关于参数输入的一些规则会不会有帮助?e、 g.强制输入为蓝色:3红色:7绿色:5黄色:2ifargs[i]==null{continue;}-这怎么可能?抱歉,修复了它。顺便说一句,新字符串[size]不会为performan提供新字符串[0]。为了获得更好的性能,我们应该在For循环之外编译该模式,并将其重新用于不同的字符串。在内部,Pattern.matches。。做这个->Pattern.compileregex.matcherinput.matchers.@DarshanMehta:谢谢你的建议。
public static void main(String[] args) {
List<Integer> numberList = new ArrayList<>();
List<String> strList = new ArrayList<>();
for (int i = 0; i < args.length; i++) {
if (Pattern.matches("-?\\d+", args[i])) {
numberList.add(Integer.parseInt(args[i]));
} else {
strList.add(args[i]);
}
}
String[] colors = strList.toArray(new String[0]);
int[] number = ArrayUtils.toPrimitive(numberList.toArray(new Integer[numberList.size()]));
}
//initialized with max length
String[] colors = new String[args.length];
int[] number = new int[args.length];
int colorIndex=-1;
int numberIndex=-1;
for(int i = 0; i < args.length; i++)
{
//if number add to number array else to colors
if(StringUtils.isNumeric(args[i])){
number[++numberIndex]=Integer.valueOf(args[i]);
}else{
colors[++colorIndex]=args[i];
}
}