Array entries, and incrementing a counter according to rules in java
I'm trying to write a method in java that will increment a counter for each time one of the following rules is satisfied when iterating through an array.
1) Two adjacent values in an array equal each other and the first value in each pair starts at an even numbered index value (0,2,4...etc)
2) For all adjacent pairs of entries, the valu开发者_Python百科es of one pair equal the values of the next pair and length(adjacent pairs of entries) mod 2 = 0 So 0,0,0,0,1,1 satisfies this rule but 0,0,0,1 does not (there are only 3 0's which violates the last condition)
So far I have this, which implements the first rule:
public class ArrayEvaluate {
public static double evaluate(int[] array)
{
double ruleSat = 0;
for(int index = 0; index < array.length; index++){
if((array[index] == array[index + 1]) &&(index%2==0)){
ruleSat++;
}
}
return ruleSat;
}
public static void main(final String[] args){
int[] array = new int[6];
array[0]= 1;
array[1]=1;
array[2]=3;
array[3]=3;
array[4]=4;
array[5]=4;
evaluate(array);
}
}
However, this does not work completely, and I'm not sure how to go about the second rule.
Thanks
public static double evaluate(int[] array) {
double ruleSat = 0;
for (int index = 0; index < array.length; index++) {
if (index != array.length-1) {
if ((array[index] == array[index + 1]) && (index % 2 == 0)) {
ruleSat++;
}
}
}
return ruleSat;
}
This will prevent the index out of bounds error your receiving
Update for second rule: This separates the logic for rule 1 and rule 2 so if you ever need to change a rule individually they are independent.
public class ArrayEvaluate {
public static double evaluate(int[] array) {
double something = 0;
int rule1Occurences = testRule1(array);
int rule2Occurences = testRule2(array);
System.out.println(rule1Occurences);
System.out.println(rule2Occurences);
return something;//not sure how rules relate
}
//This counts when array element == next array element
public static int testRule1(int[] array){
int ruleSat = 0;
for (int index = 0; index < array.length; index++) {
if (index != array.length - 1) {
if ((array[index] == array[index + 1]) && (index % 2 == 0)) {
ruleSat++;
}
}
}
return ruleSat;
}
//This blocks the pairs off in sets of two disregarding any odd parings
public static int testRule2(int[] array)
{
int rule2Sat = 0;
for (int index = 0; index < array.length; index++) {
if(index <= array.length - 4){
if (array[index] + array[index+1] == array[index+2] + array[index+3]) {
rule2Sat++;
}
}
}
return rule2Sat;
}
public static void main(final String[] args) {
int[] dataSet = new int[6];
dataSet[0] = 0;
dataSet[1] = 0;
dataSet[2] = 0;
dataSet[3] = 0;
dataSet[4] = 4;
dataSet[5] = 4;
System.out.println(evaluate(dataSet));
}
}
Since you say its not homework, this is what I would do
public class ArrayEvaluate {
public static int evaluate(int... array) {
int counter = 0;
for(int i = 0; i < array.length-1; i += 2)
if(array[i] == array[i + 1]) { // rule one
counter++;
if (i < array.length-3 &&
array[i] == array[i + 2] &&
array[i + 2] == array[i + 3]) // rule two
counter++;
}
return counter;
}
public static void main(String... args){
int test1 = evaluate(1,2,3,3,4,4);
assert test1 == 2;
int test2 = evaluate(0,0,0,0,1,1);
assert test1 == 4;
int test2 = evaluate(0,0,0,1,1,1);
assert test1 == 2;
}
}
精彩评论