Skip to main content
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
deleted 38 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

How to rewrite this method to conform Finding array indices that add up to Java coding standards?another number

I'm writing a method that takes (1) an array of intsints and (2) an intint. The purpose of the method is to find the indexesindices of the two numbers in the array that add up to the value passed in as the second parameter to the method (sum) and return those values in an int[]int[]. Exactly two numbers in the passed in array will add up to "sum", so as soon as they are identified the method should finish. I have come up with two solutions so far, but both seem ugly to me:

//this method is bad because it has multiple returns
private int[] findIndexes(int[] intAr, int sum) {    
    for (int i = 0; i < intAr.length; ++i) {
        for (int k = i + 1; k < intAr.length; ++k) {
           if(intAr[i] + intAr[k] == sum) {
              return new int[] {i + 1, k + 1}; 
           }
        }
    }
    //should never reach here
    return null;
}
//this method is bad because it uses "break" outside of a switch statement
private int[] findIndexes(int[] intAr, int sum) {
    int[] indexAr = new int[2];
    outer:
    for (int i = 0; i < intAr.length; ++i) {
        for (int k = i + 1; k < intAr.length; ++k) {
            if(intAr[i] + intAr[k] == sum) {
                indexAr[0] = i + 1;
                indexAr[1] = k + 1;
                break outer;
            }
        }
    }
    return indexAr;
}

Question: IsIs there a better way to write this method that does not violate the coding standards mentioned above?

Edit: The The coding standards I was looking at are located here: http://www.javaranch.com/styleLong.jsphere

Thanks for all the feedback!.

How to rewrite this method to conform to Java coding standards?

I'm writing a method that takes (1) an array of ints and (2) an int. The purpose of the method is to find the indexes of the two numbers in the array that add up to the value passed in as the second parameter to the method (sum) and return those values in an int[]. Exactly two numbers in the passed in array will add up to "sum", so as soon as they are identified the method should finish. I have come up with two solutions so far, but both seem ugly to me:

//this method is bad because it has multiple returns
private int[] findIndexes(int[] intAr, int sum) {    
    for (int i = 0; i < intAr.length; ++i) {
        for (int k = i + 1; k < intAr.length; ++k) {
           if(intAr[i] + intAr[k] == sum) {
              return new int[] {i + 1, k + 1}; 
           }
        }
    }
    //should never reach here
    return null;
}
//this method is bad because it uses "break" outside of a switch statement
private int[] findIndexes(int[] intAr, int sum) {
    int[] indexAr = new int[2];
    outer:
    for (int i = 0; i < intAr.length; ++i) {
        for (int k = i + 1; k < intAr.length; ++k) {
            if(intAr[i] + intAr[k] == sum) {
                indexAr[0] = i + 1;
                indexAr[1] = k + 1;
                break outer;
            }
        }
    }
    return indexAr;
}

Question: Is there a better way to write this method that does not violate the coding standards mentioned above?

Edit: The coding standards I was looking at are located here: http://www.javaranch.com/styleLong.jsp

Thanks for all the feedback!

Finding array indices that add up to another number

I'm writing a method that takes (1) an array of ints and (2) an int. The purpose of the method is to find the indices of the two numbers in the array that add up to the value passed in as the second parameter to the method (sum) and return those values in an int[]. Exactly two numbers in the passed in array will add up to "sum", so as soon as they are identified the method should finish. I have come up with two solutions so far, but both seem ugly to me:

//this method is bad because it has multiple returns
private int[] findIndexes(int[] intAr, int sum) {    
    for (int i = 0; i < intAr.length; ++i) {
        for (int k = i + 1; k < intAr.length; ++k) {
           if(intAr[i] + intAr[k] == sum) {
              return new int[] {i + 1, k + 1}; 
           }
        }
    }
    //should never reach here
    return null;
}
//this method is bad because it uses "break" outside of a switch statement
private int[] findIndexes(int[] intAr, int sum) {
    int[] indexAr = new int[2];
    outer:
    for (int i = 0; i < intAr.length; ++i) {
        for (int k = i + 1; k < intAr.length; ++k) {
            if(intAr[i] + intAr[k] == sum) {
                indexAr[0] = i + 1;
                indexAr[1] = k + 1;
                break outer;
            }
        }
    }
    return indexAr;
}

Is there a better way to write this method that does not violate the coding standards mentioned above?

The coding standards I was looking at are located here.

Tweeted twitter.com/#!/StackCodeReview/status/98552947050426368
added 140 characters in body
Source Link
sproule
  • 63
  • 1
  • 4

I'm writing a method that takes (1) an array of ints and (2) an int. The purpose of the method is to find the indexes of the two numbers in the array that add up to the value passed in as the second parameter to the method (sum) and return those values in an int[]. Exactly two numbers in the passed in array will add up to "sum", so as soon as they are identified the method should finish. I have come up with two solutions so far, but both seem ugly to me:

//this method is bad because it has multiple returns
private int[] findIndexes(int[] intAr, int sum) {    
    for (int i = 0; i < intAr.length; ++i) {
        for (int k = i + 1; k < intAr.length; ++k) {
           if(intAr[i] + intAr[k] == sum) {
              return new int[] {i + 1, k + 1}; 
           }
        }
    }
    //should never reach here
    return null;
}
//this method is bad because it uses "break" outside of a switch statement
private int[] findIndexes(int[] intAr, int sum) {
    int[] indexAr = new int[2];
    outer:
    for (int i = 0; i < intAr.length; ++i) {
        for (int k = i + 1; k < intAr.length; ++k) {
            if(intAr[i] + intAr[k] == sum) {
                indexAr[0] = i + 1;
                indexAr[1] = k + 1;
                break outer;
            }
        }
    }
    return indexAr;
}

Question: Is there a better way to write this method that does not violate the coding standards mentioned above?

Edit: The coding standards I was looking at are located here: http://www.javaranch.com/styleLong.jsp

Thanks for all the feedback!

I'm writing a method that takes (1) an array of ints and (2) an int. The purpose of the method is to find the indexes of the two numbers in the array that add up to the value passed in as the second parameter to the method (sum) and return those values in an int[]. Exactly two numbers in the passed in array will add up to "sum", so as soon as they are identified the method should finish. I have come up with two solutions so far, but both seem ugly to me:

//this method is bad because it has multiple returns
private int[] findIndexes(int[] intAr, int sum) {    
    for (int i = 0; i < intAr.length; ++i) {
        for (int k = i + 1; k < intAr.length; ++k) {
           if(intAr[i] + intAr[k] == sum) {
              return new int[] {i + 1, k + 1}; 
           }
        }
    }
    //should never reach here
    return null;
}
//this method is bad because it uses "break" outside of a switch statement
private int[] findIndexes(int[] intAr, int sum) {
    int[] indexAr = new int[2];
    outer:
    for (int i = 0; i < intAr.length; ++i) {
        for (int k = i + 1; k < intAr.length; ++k) {
            if(intAr[i] + intAr[k] == sum) {
                indexAr[0] = i + 1;
                indexAr[1] = k + 1;
                break outer;
            }
        }
    }
    return indexAr;
}

Question: Is there a better way to write this method that does not violate the coding standards mentioned above?

I'm writing a method that takes (1) an array of ints and (2) an int. The purpose of the method is to find the indexes of the two numbers in the array that add up to the value passed in as the second parameter to the method (sum) and return those values in an int[]. Exactly two numbers in the passed in array will add up to "sum", so as soon as they are identified the method should finish. I have come up with two solutions so far, but both seem ugly to me:

//this method is bad because it has multiple returns
private int[] findIndexes(int[] intAr, int sum) {    
    for (int i = 0; i < intAr.length; ++i) {
        for (int k = i + 1; k < intAr.length; ++k) {
           if(intAr[i] + intAr[k] == sum) {
              return new int[] {i + 1, k + 1}; 
           }
        }
    }
    //should never reach here
    return null;
}
//this method is bad because it uses "break" outside of a switch statement
private int[] findIndexes(int[] intAr, int sum) {
    int[] indexAr = new int[2];
    outer:
    for (int i = 0; i < intAr.length; ++i) {
        for (int k = i + 1; k < intAr.length; ++k) {
            if(intAr[i] + intAr[k] == sum) {
                indexAr[0] = i + 1;
                indexAr[1] = k + 1;
                break outer;
            }
        }
    }
    return indexAr;
}

Question: Is there a better way to write this method that does not violate the coding standards mentioned above?

Edit: The coding standards I was looking at are located here: http://www.javaranch.com/styleLong.jsp

Thanks for all the feedback!

Post Migrated Here from stackoverflow.com (revisions)
Source Link
Loading