7. แถวลำดับ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 7-6  เมท็อดหาผลรวมของจำนวนเต็มในอาเรย์ที่ได้รับ
public class TestMethod {
  public static void main(String[] args) {
    int[] d = {1,3,5,7};
    System.out.println( sum(d) );
    System.out.println( sum(new int[]{1,2,3}) );
  }
  public static int sum(int[] data) {  
    int sum = 0;
    for(int i=0; i<data.length; i++) {
      sum += data[i];
    }
    return sum;
  }
}
©2009 S.Prasitjutrakul