减去数组中的元素

时间:2014-07-28 10:28:52

标签: java android android-listview android-cursoradapter

我有一个数组,其值为80 82 84 90 94是否可以减去这些值,因此输出可能是0 2 2 6 4? 我编辑了这个问题:现在我想在android游标适配器中使用它,但是当它到达差异的计算时我得到索引超出界限。

   public void bindView(View view, Context context, Cursor cursor) {

        // here we are setting our data
        // that means, take the data from the cursor and put it in views

            double weight = cursor.getDouble(cursor
                .getColumnIndex(DbHelper.ENTRY_USER_WEIGHT));



            int count=cursor.getCount();
            Double[] input = new Double[count];
            // Obtaining the number of records


            System.out.println("number of records "+input.length);
            // Array for storing differences

            double[] difference= new double[count ];

            difference [0] = 0; // First record difference is 0 only
            int i;
             // Looping number of records times
           for(  i=0; i<count-1 ;i++)
             {        

                    input[i]=weight;
                    System.out.println("i value"+i);
                  System. out.println(""+input[i]);
                 // Difference = next record - current record
               difference [i]= input [i+1] - input[i];
               //  System.out.println ("Difference between "+input [i+1]+ " and  "+input[i] + " is : " +difference[i]);

             } 

3 个答案:

答案 0 :(得分:1)

        // Setting the input array.
        int input[]=     {80, 82, 84, 90, 94};

        // Obtaining the number of records
        int noOfRecords = input.length;

        // Array for storing differences
        double[] difference= new double[noOfRecords ];

        difference [0] = 0; // First record difference is 0 only

         // Looping number of records times
         for( int i=0; i < noOfRecords -1 ;i++)
         {       
             // Difference = next record - current record
             difference [i+1]= input [i+1] - input[i];
             System.out.println ("Difference between "+input [i+1]+ " and  "+input[i] + " is : " +difference[i+1]);
         } 

         System.out.println("My final difference array Output is : "+java.util.Arrays.toString( difference ));

<强>输出:

Difference between 82 and  80 is : 2.0
Difference between 84 and  82 is : 2.0
Difference between 90 and  84 is : 6.0
Difference between 94 and  90 is : 4.0
My final difference array Output is : [0.0, 2.0, 2.0, 6.0, 4.0]

如果您将<{1}}替换为

double[] difference = new double[noOfRecords ];

您可以完全按照自己的意愿获得输出:

int [] difference = new int [noOfRecords];

答案 1 :(得分:1)

逻辑: 对于数组Arr[] = {80 82 84 90 94} 必需输出= {0,2,2,6,4}

Sol:

output[0] = 0; 
for( i=1;i<cursor.getCount();i++)
{       
    output[i] = Arr[i]-Arr[i-1];
}  

请注意,输出数组元素是通过使用前一个索引处的元素减去当前索引元素获得的。 实施例82-80 = 2,84-82 = 2,90-84 = 6和94-90 = 4

答案 2 :(得分:0)

您可以从下一个数字中减去一个数字。

int[] numbers={80, 82, 84, 90, 94};
for (int i = 0; i < numbers.length; i++) {
    if(i < numbers.length - 1)
        System.out.println(numbers[i + 1] - numbers[i]);
}
}

输出 -

2
2
6
4