Arrays of Arrays

Arrays of Arrays

Is it possible to have an array of character arrays? What about anarray of multidimensional character arrays?

Java arrays are first-class objects. They can be referenced byObject reference variables, and unlike C/C++, do not correspond to rawchunks of memory. When you create an array, its contents are notspecified unless the array is of a native type. The contents of the array are null object references (thereis a short-hand initializer syntax, but that’s just a syntacticconvenience). You must explicitly initialize the elements of thearray.

The types of these elements will depend on the type ofthe array you declared. If you want to store any arbitrary classinstance in the array, including other arrays, you may declare thearray to be of type Object[]. But if you want to limit the type of array to store, say, arrays of characters, you would declare itto be of type char[][]. For example:

    char[][] bar;    bar = new char[50][];    for(int i=0; i < bar.length; ++i) {      bar[i] = new char[5];      for(int j=0; j < bar[i].length; ++j)        bar[i][j] = 'a';    }

You can extend this to store multidimensional character arrays inan array.

See also  Essential Measures for Safeguarding Your Digital Data

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist