从不同数组填充数组时出现JAVA程序错误

从不同数组填充数组时出现JAVA程序错误,java,arrays,list,int,populate,Java,Arrays,List,Int,Populate,当从inputList填充oddList、evenList和negativeList时,程序只填充一个int,而不是inputList数组中所有对应的int。输出应该是每个数组的列表,其编号对应于其标题。用户将数字输入到inputList数组中,然后从那里确定它是奇数、偶数还是负数,然后填充相应的数组。 即,evenList由inputList中的偶数整数填充 公共类项目Tenone { publicstaticvoidmain(字符串[]args) { int[]inputList=新的int

当从inputList填充oddList、evenList和negativeList时,程序只填充一个int,而不是inputList数组中所有对应的int。输出应该是每个数组的列表,其编号对应于其标题。用户将数字输入到inputList数组中,然后从那里确定它是奇数、偶数还是负数,然后填充相应的数组。 即,evenList由inputList中的偶数整数填充

公共类项目Tenone {

publicstaticvoidmain(字符串[]args)
{
int[]inputList=新的int[10];
int[]oddList=null;
int[]evenList=null;
int[]negativeList=null;
int evenCount=0;
int oddCount=0;
int negCount=0;
扫描仪输入=新扫描仪(System.in);
//System.out.println(“输入任意十个整数:”);
for(int list=0;list

}

这里我以evenList为例,但其他两个数组也是如此

在代码中,迭代输入列表并检查偶数。如果为偶数,则将整个evenList数组设置为find值,而不是仅设置单个元素

您可以通过在外部循环之外声明一个int来解决这个问题,这个int跟踪输入的偶数。例如:

int evenIndex = 0;
for(int l = 0; l < inputList.length; l++)
{
    if((inputList[l] % 2) == 0)
    {
        evenList[evenIndex++] = inputList[l];
    }
    /*Other code*/
}
int-evendex=0;
for(int l=0;l
您在上一个循环中也犯了错误。迭代negativeList,但使用evenList的大小。当negativeList小于evenList时,这将导致ArrayIndexOutOfBoundsException

int evenIndex = 0;
for(int l = 0; l < inputList.length; l++)
{
    if((inputList[l] % 2) == 0)
    {
        evenList[evenIndex++] = inputList[l];
    }
    /*Other code*/
}