Can anyone explain in detail why arrays are implemented as objects in java.And wat do we mean by arrays of arrays in java.....am not getting it completely....how does arrays differ from other languages when we use a 2d array for example.pls explain with example how a 2d array will be stored in memory.I studied Baldwin's array tutorials....it was good...but there was no diagram to explain how they are stored in memory.....am looking out for a good explanation.....am very fascinated to learn about the intricacies of java arrays
Note: you probably should have done this on your own, but perhaps you didn't know (though you could have looked things up.
Hello, here is an example program using an array of an array:
- public class arrayTest {
-
-
public static void main(String args[]) {
-
int topArrayLength = 5; // Stays the same
-
int bottomArrayLength = 3; // As Jos said, doesn't have to stay the same, though I did keep it the same in this example
-
int[][] integerArrayOfArray = new int[topArrayLength][bottomArrayLength];
-
-
for (int i = 0; i < topArrayLength; i++) {
-
for (int j = 0; j < bottomArrayLength; j++) {
-
integerArrayOfArray[i][j] = i * j;
-
}
-
}
-
-
for (int i = 0; i < topArrayLength; i++) {
-
for (int j = 0; j < bottomArrayLength; j++) {
-
System.out.println("The array of array of integers at position " + i + " at position " + j + " equals " + integerArrayOfArray[i][j]);
-
}
-
}
-
}
-
}
and here's the output:
The array of array of integers at position 0 at position 0 equals 0
The array of array of integers at position 0 at position 1 equals 0
The array of array of integers at position 0 at position 2 equals 0
The array of array of integers at position 1 at position 0 equals 0
The array of array of integers at position 1 at position 1 equals 1
The array of array of integers at position 1 at position 2 equals 2
The array of array of integers at position 2 at position 0 equals 0
The array of array of integers at position 2 at position 1 equals 2
The array of array of integers at position 2 at position 2 equals 4
The array of array of integers at position 3 at position 0 equals 0
The array of array of integers at position 3 at position 1 equals 3
The array of array of integers at position 3 at position 2 equals 6
The array of array of integers at position 4 at position 0 equals 0
The array of array of integers at position 4 at position 1 equals 4
The array of array of integers at position 4 at position 2 equals 8
How is that different from other languages?
What language exactly? C++ is hardly different because they're both claimed to be "object oriented".
This brings up the answer to your other question...
Why are arrays implemented as objects in java?
Because EVERYTHING (with the exception of primitive types like int, boolean, double, char, is that all of them?) in java is implemented as an object. After all, it's not called "object oriented" for nothing. That's how the system works... it's not a bad system (some might argue this).
As for memory mapping, I don't know this 100% but I'm pretty sure it works like this (it's simpler then you made it out to be):
If this array in question is the one we made above where an int (represented by 8 bits or one byte) looks like one rectangle shown below:
[--------][--------][--------]
[--------][--------][--------]
[--------][--------][--------]
[--------][--------][--------]
[--------][--------][--------]
edit (if the arrays had different sizes it would look something like this):
[--------][--------][--------]
[--------][--------]
[--------][--------][--------][--------]
[--------][--------][--------][--------][--------]
[--------][--------][--------]
I guess it's possible that the organization is simply done via software and it might actually look like this:
[--------][--------][--------][--------][--------][--------][--------][--------][--------][--------][--------][--------][--------][--------][--------]
Not so important in my mind though...
Hope that helped you out,
-blazed