Array out-of-bounds exceptions are really annoying and annoying. Collections are great tools, and it's a little overkill to handle small data in collections every time.
Below is a simple Intarrayutil tool class (humble degree, oneself all want to cover face, do not know to take this thing water own blog, will not be beaten by people,/manual funny).
PackageCn.util;/*** Integer Array tool class: Create and set values for one-dimensional arrays, create and set values for two-dimensional arrays. * * @authorJxlys *@version1.0*/ Public classIntarrayutil {/*** Gets the value of the one-dimensional array at the specified position. An error in the query returns-1. * * @paramIarr * Original array *@paramIndex * Indexes *@return */ Public Static intGetint[] Iarr,intindex) { returnNoarrayindexoutofbounds (iarr.length, index)? Iarr[index]:-1; } Public Static intGetint[] Iarr,intXinty) {returnNoarrayindexoutofbounds (iarr.length, y) && noarrayindexoutofbounds (iarr[y].length, x)? IARR[Y][X]:-1; } /*** Create a one-dimensional array whose parameters are the sequential input values. Variable parameter ( how many values are there for the number of arguments) **/ Public Static int[] Newone (int... values) { returnvalues; } /*** Creates a one-dimensional array of the specified length, and returns null if the length is not greater than 0. * * @paramlength * array lengths. */ Public Static int[] Newonelength (intlength) { returnLength>0?New int[Length]:NULL; } /*** Create a two-dimensional array that specifies the number of one-dimensional arrays and the length of one-dimensional arrays. * * @paramx * Length of one-dimensional array *@paramy * The number of one-dimensional arrays. */ Public Static int[] Newtwolength (intXinty) {returnX>0 && y>0?New int[Y] [x]:NULL; } Private Static BooleanNoarrayindexoutofbounds (intLengthintIndex) {//detecting whether an array is out of bounds returnIndex >= 0 && index < length?true:false; } /*** Set all values of the array. * * @paramIarr * Original array *@paramValue * Values*/ Public Static voidSetAll (int[] Iarr,intvalue) { for(inti = 0; i < iarr.length; i++) {Iarr[i]=value; } } /*** Modifies the value of the specified index of a one-dimensional array and does not modify if the index is out of bounds. * * @paramIarr * Original array *@paramIndex * Indexes *@paramValue * Values*/ Public Static voidSetint[] Iarr,intIndexintvalue) { if(Noarrayindexoutofbounds (iarr.length, index)) Iarr[index]=value; } /*** Sets the value of the specified index for the two-dimensional array. * * @paramIarr * Two-dimensional array *@paramx * Index in one-dimensional array *@paramy * Index of two-dimensional arrays *@paramValue * Values*/ Public Static voidSetint[] Iarr,intXintYintvalue) { if(Noarrayindexoutofbounds (iarr.length, y) &&noarrayindexoutofbounds (iarr[y].length, X)) {Iarr[y][x]=value; } } PrivateIntarrayutil () {//Privatization Construction Method Throw Newruntimeexception (); }}
Simple tool class for the int array of Java IntArrayUtil1.0