473,395 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Arrays

73
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
May 30 '07 #1
9 1256
JosAH
11,448 Expert 8TB
Arrays are not so special:

1) Arrays are Objects because everything is an Object in Java.

2) Arrays store elements of type T and that makes the Array a T[].

3) If T happens to be an array of type T again, that makes the first array a T[][]
array, in other words the array is equivalent to a two dimensional array but
internally it's an array of arrays where each of the arrays store type T objects.

Especially point 3) allows for the fact that an array of arrays doesn't impose the
restriction that all arrays need to have the same size, i.e. two (or higher)
dimensional arrays can be 'ragged'. Here's an example:
Expand|Select|Wrap|Line Numbers
  1. int[][] array= new array[3][]; // an array of int arrays
  2. ...
  3. for (int i= 0; i < array.length; i++) { // loop over all arrays
  4.    arrays[i]= new int[i+1]; // an array that can store i ints;
  5.    for (int j= 0; j < arrays[i].length; j++) // fill the array
  6.       arrays[i][j]= j+1;
  7. }
  8. ...
  9. print(int[] arrray) { // print a one dimensional array
  10.    for (int i= 0; i < array.length; i++)
  11.       System.out.println(array[i]+" ");
  12.    System.out.println();
  13. }
  14. print(int[][] array) { // print the array of int arrays
  15.    for (int i= 0; i < array.length; i++)  // loop over the arrays
  16.       print(array[i]);  // and print the one dim array
  17. }
Play with it a bit and you'll understand.

kind regards,

Jos
May 30 '07 #2
blazedaces
284 100+
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:
Expand|Select|Wrap|Line Numbers
  1. public class arrayTest {
  2.  
  3.     public static void main(String args[]) {
  4.         int topArrayLength = 5; // Stays the same
  5.         int bottomArrayLength = 3; // As Jos said, doesn't have to stay the same, though I did keep it the same in this example
  6.         int[][] integerArrayOfArray = new int[topArrayLength][bottomArrayLength];
  7.  
  8.         for (int i = 0; i < topArrayLength; i++) {
  9.             for (int j = 0; j < bottomArrayLength; j++) {
  10.                 integerArrayOfArray[i][j] = i * j;
  11.             }
  12.         }
  13.  
  14.         for (int i = 0; i < topArrayLength; i++) {
  15.             for (int j = 0; j < bottomArrayLength; j++) {
  16.                 System.out.println("The array of array of integers at position " + i + " at position " + j + " equals " + integerArrayOfArray[i][j]);
  17.             }
  18.         }
  19.     }
  20. }
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
May 30 '07 #3
jyohere
73
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:
Expand|Select|Wrap|Line Numbers
  1. public class arrayTest {
  2.  
  3.     public static void main(String args[]) {
  4.         int topArrayLength = 5; // Stays the same
  5.         int bottomArrayLength = 3; // As Jos said, doesn't have to stay the same, though I did keep it the same in this example
  6.         int[][] integerArrayOfArray = new int[topArrayLength][bottomArrayLength];
  7.  
  8.         for (int i = 0; i < topArrayLength; i++) {
  9.             for (int j = 0; j < bottomArrayLength; j++) {
  10.                 integerArrayOfArray[i][j] = i * j;
  11.             }
  12.         }
  13.  
  14.         for (int i = 0; i < topArrayLength; i++) {
  15.             for (int j = 0; j < bottomArrayLength; j++) {
  16.                 System.out.println("The array of array of integers at position " + i + " at position " + j + " equals " + integerArrayOfArray[i][j]);
  17.             }
  18.         }
  19.     }
  20. }
and here's the output:


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
Thanx a lot....But, i wanted to know why they call arrays of arrays....

how the array reference refer an array object.

if we say Object[] [] ref=new Object[2][3]

how many objects will totally be in memory and why
May 30 '07 #4
JosAH
11,448 Expert 8TB
Thanx a lot....But, i wanted to know why they call arrays of arrays....

how the array reference refer an array object.

if we say Object[] [] ref=new Object[2][3]

how many objects will totally be in memory and why
There are three objects after you've instantiated your 'ref' object:

1) two arrays; one for each row
2) and one array named 'ref'.

The two elements of array 'ref' point (or refer) to the two rows. Note that the slots
in the two rows are all equal to null at that moment.

kind regards,

Jos
May 30 '07 #5
jyohere
73
There are three objects after you've instantiated your 'ref' object:

1) two arrays; one for each row
2) and one array named 'ref'.

The two elements of array 'ref' point (or refer) to the two rows. Note that the slots
in the two rows are all equal to null at that moment.

kind regards,

Jos
Thanx .....so the above code is equivalent to:

Object[] ref=new Object[2];

ref[0]=new Object[3];
ref[1]=new Object[3];

Am I right?
May 30 '07 #6
JosAH
11,448 Expert 8TB
Thanx .....so the above code is equivalent to:
Expand|Select|Wrap|Line Numbers
  1. Object[] ref=new Object[2];
  2.  
  3. ref[0]=new Object[3];
  4. ref[1]=new Object[3];
Am I right?
Yep, except that you forgot pairs of brackets in the first line:
Expand|Select|Wrap|Line Numbers
  1. Object[][] ref= new Object[2][];
kind regards,

Jos
May 30 '07 #7
the arrays in java rae just like the on's in c++ except they use new keyword to instensiate !!!

(ofcorce only by the appearance. in java the type checking automatic garbage collection and overflows are still checked for arrays as well, unlike in c++)
May 30 '07 #8
jyohere
73
the arrays in java rae just like the on's in c++ except they use new keyword to instensiate !!!

(ofcorce only by the appearance. in java the type checking automatic garbage collection and overflows are still checked for arrays as well, unlike in c++)

Jos wat i did was right.....


Object[] ref=new Object[2];

ref[0]=new Object[3];
ref[1]=new Object[3];

The above code is equal to

Object[][] ref=new Object[2][3];

Is it not
May 31 '07 #9
jyohere
73
Jos wat i did was right.....


Object[] ref=new Object[2];

ref[0]=new Object[3];
ref[1]=new Object[3];

The above code is equal to

Object[][] ref=new Object[2][3];

Is it not

Sorry i was wrong.....u were correct
May 31 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.