Java.lang.ArrayIndexOutOfBoundsException

Source: Internet
Author: User

Idle egg ache, looked at the next Yang Hui Triangle Code, also since this found Yang Hui triangle is god same thing, if you see this blog, please you must Baidu under Yang Hui Triangle

The Java code output is as follows

1 /**2  * 3  */4  PackageCom.yuxi.demo;5 6 /**7 * @ClassName: Yanghuitriangle8 * @Description: TODO (Here's a word describing the role of this class)9  * @authorYuxiTen * @date 2015-6-28 a.m. 8:52:52 One  * A  */ -  Public classYanghuitriangle { -      Public Static voidMain (string[] args) { the         inttriangle[][]=New int[12] [];//creating a two-dimensional array -         //traversing the first layer of a two-dimensional array -          for(inti = 0; I <= triangle.length; i++) {//here is the key to generating the exception exception in thread "main" Java.lang.arrayindexoutofboundsexception:12 -                                                     //At Com.yuxi.demo.YanghuiTriangle.main (yanghuitriangle.java:20) +  -  +       //System.out.println ("Triangle.length is" +triangle.length); Atriangle[i]=New int[I+1];//Initialize the size of the second-level array at             //traversing a second-level array -              for(intj=0;j<=i;j++){ -                 //assigns an array element on both sides to a value of 1 -                 if(i==0| | j==0| | j==i) { -Triangle[i][j]=1; -}Else{//other values are calculated by formula inTriangle[i][j]=triangle[i-1][j]+triangle[i-1][j-1]; -                 } toSystem.out.print (triangle[i][j]+ "\ t");//Output array elements +             } -System.out.println ();//line Break the         } *     } $}
View Code

The output is as follows

1121 131 2 141 3 3 151 4 6) 4 161 5 10 10 5 171 6 15 20 15 6 181 7 21 35 35 21 7 191 8 28 56 70 56 28 8 1Ten1 9 36 84 126 126 84 36 9 1 One1 10 45 120 210 252 210 120 45 10 1 A1 11 55 165 330 462 462 330 165 55 11 1 -Exception in thread "main" java.lang.arrayindexoutofboundsexception:12 -At Com.yuxi.demo.YanghuiTriangle.main (yanghuitriangle.java:20)
View Code

In the code for (int i = 0; I <= triangle.length; i++), the <= symbol is used, because in Java, the array subscript = array length, it warns

Java.lang.ArrayIndexOutOfBoundsException

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.