473,748 Members | 6,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arrays

73 New Member
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....h ow 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 1268
JosAH
11,448 Recognized Expert MVP
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 Contributor
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....h ow 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 New Member
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 Recognized Expert MVP
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 New Member
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 Recognized Expert MVP
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
shanakard
13 New Member
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 New Member
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 New Member
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
2847
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 > >pointer-to-array-of-size-N-of-type-T (which is fine) and not having type > >array-of-size-N-of-type-T (with some exceptions, which is curious). > > So far > >the consensus seems to be that while everyone is aware of this no one knows
21
3934
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 "browse" it). So I expected that when I run this program, I get both c1.A and c2.A pointing to the same address, and changing c1.A means that also c2.A changes too. ----- BEGIN example CODE -----------
5
29249
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); pfiles = ld.GetFiles("*.aspx.resx|") + ld.GetFiles("*.ascx.resx") + ld.GetFiles("*.master.resx"); but of course there is no + operation allowed on the FileInfo arrays returned by the GetFiles method.
3
2844
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; } *arrays; It's supposed to be a structure containing an array of chars, an array of ints and an int. I declare functions like this : arrays *parseline(char *line, int N)
1
8705
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 Framework are said to have an element type. http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
41
4969
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 the hash are alphabetically sorted if the key happens to be alpha numeric. Which I believe makes sense because it allows for fast lookup of a key.
6
13159
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: the two arrays {"one","two","three"},{"red","green","blue} the result one red one green
1
2449
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 of different lengths (e.g. usually between 25 and 45 rows. The columns in each file have the same length). The text files have been numbered sequentially e.g. cb0, cb1, cb2 and so on. I would like to read the data from each text file into...
16
2545
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 at just comp.lang.c++. If one of these groups is too inappropriate, just take it off from where you send your replies.) Hi.
29
35480
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 multi-dimensional arrays work, b) how to call a function with a multi-dimensional array, c) how to return a multi-dimensional array from a function, or d) how to read and write arrays from a disc file. Note to C++ programmers: You should be using...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9316
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.