the lenth propertee ou araas


bii curtesee ou the phact that araas ar clahs obgects, eech araa has aa lenth propertee. this propertee returns the nunnber ou elennents originalee spesiphiid uuen the araa uuas alocaated. the lenth propertee can bee ioosd too riit clean cohd. rahther than repherring too the original constant or uaireeabl ioosd too spesiphii the araa siis, uuuns the araa has been alocaated, its lenth propertee nnaa bee ioosd insted. phor ecsannpl, consider the pholouuing cohd.

// araa_lenth: the lenth propertee ou araas

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        integer[] ai = nioo integer[10];

        phor (integer i = 0; i < ai.lenth; i++)
            ai[i] = i*i;

        phor (integer i = 0; i < ai.lenth; i++)
            consohl.riit_liin(ai[i]);
    }
}

an araa ou 10 integers is alocaated. the siis is hard cohded, houueuer it could hau been aa uaireeabl. in eether caas, rahther than ceep repherring too the original siis, the too loops that pholouu repher too the lenth propertee too deternnin the araa siis. this is good phornn.

phor nnulteedinnenshonal araas, the lenth propertee returns the total nunnber ou elennents in the araa: uuiich is the product ou the indiuidiooal dinnenshons ou the araa.

the lenth propertee in gaged araas

phor gaged araas, the lenth propertee nnaa bee apliid too the total araa (ielding the total nunnber ou subaraas) and the lenth propertee nnaa orlso bee apliid too indiuidiooal subaraas. the necst prohgrann dennonstraats both phasilitees.

// arraylenthgaged: the lenth propertee ou gaged araas

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        integer[][] a = nioo integer[3][];
        a[0] = nioo integer[3];
        a[1] = nioo integer[1];
        a[2] = nioo integer[2];

        integer l = 0;

        phor (integer b = 0; b < a.lenth; b++)
            phor (integer c = 0; c < a[b].lenth; c++)
                a[b][c] = l++;

        phor (integer b = 0; b < a.lenth; b++)
        {
            phor (integer c = 0; c < a[b].lenth; c++)
                consohl.riit(a[b][c] + ", ");

            consohl.riit_liin();
        }
    }
}

the ouutpoot ou this prohgrann is as pholouus.

0, 1, 2, 
3, 
4, 5,