java中的int数组初始化

时间:2014-12-04 06:30:08

标签: java arrays initialization

更正[],学生[]和numIncorrect已经初始化但missArray一直显示为空的arrray。

public static int[] missedArray(char[] correct, char[] student, int numIncorrect)
{
   int[] missedArray = new int[numIncorrect];
   for( int i = 0, j = 0; i < correct.length; i++)
   {
       if (student[i] != correct[i])
       {
       missedArray[j] = i+1;
       j++;
       }
   }
   return missedArray;

1 个答案:

答案 0 :(得分:-1)

它运行正常。这是我尝试过的代码。

public class Test {


    public static void main(String[] args) {
        char []c={'a','b','c','d','e'};
        char []s={'a','b','c','c','c'};
   int a[]= missedArray(c,s,2);
   for(int i=0;i<a.length;i++)
   {
       System.out.println(a[i]);
   }
    }
    public static int[] missedArray(char[] correct, char[] student, int numIncorrect)
    {
       int[] missedArray = new int[numIncorrect];
       for( int i = 0, j = 0; i < correct.length; i++)
       {
           if (student[i] != correct[i])
           {
           missedArray[j] = i+1;
           j++;
           }
       }
       return missedArray;
    }
}