开发者

Is It Possible to Point to Multiple Arrays Within Arrays?

开发者 https://www.devze.com 2023-03-17 13:53 出处:网络
SO for a project I am doing, I would like to be able to create a multidimensional array which would look like:

SO for a project I am doing, I would like to be able to create a multidimensional array which would look like:

private static String[][] docArray = new String[6][];

For that array I'd establish something like:

docArray[0].equals(headingValue);
.
.
.
docArray[5].equals(anotherValue);

Then say I want to do something like have index 3 point to a child array, logically I'd think you'd do something like the following (although I know it's not the correct syntax)...

docArray[3][1] = new String[6][];

So am I 开发者_运维技巧correct in my logic thus far, and how do I continue about adding pointers to new arrays? Thanks in advance for any help.


The problem with this is that if docArray is a String[][], then docArray[3][1] should be a String, not a String[][].

What you could do is type docArray as an Object[][]; this would allow you to do what you want since every String is an Object and a jagged String[][] array is also an Object.


since docArray[3] is also an array, you can do....

docArray[3] = new string[4]; //4 for example

If memory serves correctly, this is called a jagged array.


I think what you're looking for is the following syntax:

String[][] s2d = new String[3][]; // creates a 2-Dimensional Array with 3 rows (or columns, depending on how you index/address
s2d[0] = new String[4] // creates a new 1-Dimensional Array with 4 columns (at row 0)
s2d[1] = new String[26] // creates a new 1-Dimensional Array with 26 columns (at row 1)
s2d[0][0] = "This is a cell at row 0, column 0"


I think the key concept here is that no matter how you declare it, in Java, an array of anything is simply a normal object, defined as a series of elements of a given type, and therefore arrays of objects can contain other arrays.

There is no support in the JVM for multidimensional arrays to be handled any differently. So there are just two types of array:

The F[] array where F is a fundamental type (int, float, etc.) The O[] array where O is any other type - including arrays ( O = F[], or O = O'[] )

So, for your question, you can make arrays of arrays, nothing will stop you. Just realize that you are simply accessing one dimensional arrays of object references.

The syntax ((array[i][j])[m][n]) is no different to (((((array[a])[b]))[c])[d])

From reading your question and earlier answers, I would offer the following advice: stay as strongly typed as possible, because you can only discard typing information as you delve deeper into your multi-dimensional array, you can never add it in. In a simple form you could represent your document using multiple simple types:

class Line extends ArrayList<String> {}

class Paragraph extends ArrayList<String> { public Line[] getLines(); } 
class TextParagraph extends Paragraph {}
class QuotedParagraph extends Paragraph {} 
class HeadingParagraph extends Paragraph {}

class Document { public static void main (String[] args) { 
  Paragraph[] multiDiDoc = new Paragraph[10]; 
  multiDiDoc[0] = new HeadingParagraph();
} }

Using an Object Model, you can preserve generality ('HeadingParagraph', 'QuotedParagraph', 'TextParagraph' types), and support specificity (arbitrary objects of type Object are not allowed).

If you stick with raw Objects, as your client application gets bigger you'll be wondering more and more often whether you are going to get hit a class cast exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消