1

Is it possible to declare an array of type String[] array.
I think not, so how you go about it?

I have a for loop in which each iteration returns another array of String type. So that i need to store all those arrays.

EDIT:

Can someone tell me how to print out a two dimensional string array.
The problem is Array[i][j], now here "i" is fixed but "j" keeps changing dpeneding upon the size of array returned in above method.

So how can i proceed further. Any simple printing idea would be a great help.

EDIT:

How can i get the size of [j] index. Array.length; returns the size of [i] index.

EDIT:

Here is how i did it:

for (int i=0; i< files.length; i++){
    for (int j=0; j<files[i].length; j++){
            System.out.println("["+i+"]["+j+"] = "+ files[i][j]);
        }
    }
}
4
  • There is no such thing as "the size of [j] index". Every String[] in a String[][] may have its own, separate length. Commented Dec 27, 2010 at 16:28
  • @Karl, So that means there is no way to check out how many elements are stored against each [i]? If this is the case, i can not have any general set of code to display the results. Everytime i get NullPointer Exception. I think i would have to use Linked LIsts here. Commented Dec 27, 2010 at 16:35
  • @Karl, you were WRONG. I figured out the way. In each iteration i can use Array[i].length; to get size of [j] index. I would appreciate if you could avoid giving wrong idea. If you don't know something, please don't raise confusion and time wastage for others. Commented Dec 27, 2010 at 16:39
  • I said nothing wrong. Read it again: "Every String[] in a String[][] may have its own, separate length". That is why, "in each iteration, [you] can use Array[i].length" to get the length of that String[]. I know exactly what I'm talking about, and your time was very well spent, as you figured out what to do. Your terminology was simply a bit off. BTW, NullPointerExceptions have nothing to do with the problem of figuring out how long each row is. They came about because a String[][] holds String[] objects, and a String[] can be null, and x[i] is invalid when x is null. Commented Dec 27, 2010 at 17:55

5 Answers 5

2

You might be referring to 2 dimensional arrays. A very quick tutorial is available here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, what confused me i thought by multidimensional they are really "multidimensional". It seems in java they really an array of another array.
1

Yes - it's String[][]. You can also have 3-dimensional array with [][][]

On the other hand, you can use a List<List<X>>.

Comments

1
String[][] stringArrayArray = new String[5][];
stringArrayArray[0] = new String[10];

etc

4 Comments

stringArrayArray[0][] = new String[10] wrong usage. it should be like that stringArrayArray[0] = new String[10]
I think two nested for loops can do the job. How you go about checking the size of 2nd index in a two dimensinal array.
@Gursel Koca - Thanks. I was fixing it as you were commenting.
@David - The second dimension can vary for each member of the first. That is, stringArrayArray[0].length could be 4 and stringArrayArray[1].length could be 19.
1

2D arrays are arrays of arrays see here http://www.willamette.edu/~gorr/classes/cs231/lectures/chapter9/arrays2d.htm

Comments

1

It can be done.

String[] array = new String[10]; //Array for ten String objects.

String[][] arrayOfArrays = new String[10][]; //Array for ten String arrays.


arrayOfArrays[0] = array; // Under index 0 we assign the reference to array.

Another approach is to use List class.

   List<String[]> listofArrays = new ArrayList<String[]>();

   listofArrays.add(array):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.