devxlogo

Arrays of Arrays

Arrays of Arrays

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

Answer:
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 

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

devx-admin

Share the Post: