• Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing memory of address  RSS feed

 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanted to know if my understanding of array is correct. Here you have some code and I try to draw a picture of memory. Is this correct?

int[ ] m = new int[2];
m[0] = 0;
m[1] = 1;
int[ ] p = m;
p[0] = 4;
p[0] = 5

**My attempt in “drawing a picture of memory”**
m ---> [empty][empty]
m ---> [0][empty]
m ---> [0][1]
p=m ---> [0][1] **Here, array variable p references array variable m. Both are referencing the same address. Whatever changes happens in array p will also happen in array m.
p ---> [4][1] and m ---> [4][1]
p ---> [4][5] and m ---> [4][5]
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such thing as 'empty' for array elements. When you create an array each element is initialized to the default value of whatever type it holds. In the case of int the default value is zero. So the first statement is:

m -> int[0][0]
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conrado Sanchez wrote:int[ ] m = new int[2];
m[0] = 0;
m[1] = 1;
int[ ] p = m;
p[0] = 4;

The part above looks valid code. You created an array of size 2, which can hold maximum 2 elements of type "int" in it. Array can be accessed by reference variable "m" and specified index based on the elemnt position you want to access to. Later you declared reference variable "p" which is of type array and initialized it with "m", which means, from that time your reference variable "p" refers to the same array object as reference variable "m". So, once you assign array position with different value than you had before by doing it "p[0] = 4", you'll see that effect by accessing array via reference variable "m[0]".

Everything what's below that code snippet didn't manage to understand what have you tried to tell. Please try different approach in explaining what you meant.
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Conrado Sanchez wrote:int[ ] m = new int[2];
m[0] = 0;
m[1] = 1;
int[ ] p = m;
p[0] = 4;

The part above looks valid code. You created an array of size 2, which can hold maximum 2 elements of type "int" in it. Array can be accessed by reference variable "m" and specified index based on the elemnt position you want to access to. Later you declared reference variable "p" which is of type array and initialized it with "m", which means, from that time your reference variable "p" refers to the same array object as reference variable "m". So, once you assign array position with different value than you had before by doing it "p[0] = 4", you'll see that effect by accessing array via reference variable "m[0]".

Everything what's below that code snippet didn't manage to understand what have you tried to tell. Please try different approach in explaining what you meant.


Here is the revised work. All I changed was the word empty to 0, which is what Mike. J. Thompson pointed out. Other than that I don't see anything that's wrong with it.


 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does
m --> [0][0]
mean? The memory address of an array is not the same as that of its first element. It might be in some language, but not in Java®. The idea in Java® is to hide all that sort of detail from the user completely. You also seem to have two indices and you only had one index before.
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What does
m --> [0][0]
mean? The memory address of an array is not the same as that of its first element. It might be in some language, but not in Java®. The idea in Java® is to hide all that sort of detail from the user completely. You also seem to have two indices and you only had one index before.


Please note that what I have below is not actual java codes. It is "**My attempt in “drawing a picture of memory”**" which corresponds to the java codes. I guess it's my fault because I used put it in a java format. But it's not java codes. Just a "picture" of what's happening to the actual java codes that I posted along with this "picture".

m ---> [0][0]
m ---> [0][0]
m ---> [0][1]
p=m ---> [0][1] **Here, array variable p references array variable m. Both are referencing the same address. Whatever changes happens in array p will also happen in array m.
p ---> [4][1] and m ---> [4][1]
p ---> [4][5] and m ---> [4][5]
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conrado Sanchez wrote:Here is the revised work. All I changed was the word empty to 0, which is what Mike. J. Thompson pointed out. Other than that I don't see anything that's wrong with it.
I worked hard and it seems I understood what you meant. But as Campbell Ritchie pointed out, it seems you mixing indexes and elements.

When you write A[0][1] or something similar, is a big chance that most of the people will understand that as an element which is present in array of arrays at indices 0 and 1. If I'm not mistaken, you're trying to show how elements looks inside array after you assign some values to a certain array positions. Mike was right, all array elements initially being assigned to 0's unless you explicitly assign them to a different values.

Probably less confusion would be if you would do your show in this way:
m[0] = 0; results array "m" elements to be [0, 0]
m[1] = 1; results array "m" elements to be [0, 1]
int[] p = m; results array "p" elements to be [0, 1]
p[0] = 4; results arrays "m", "p" elements to be [4, 1]
p[0] = 5; results arrays "m", "p" elements to be [5, 1]

Sorry I didn't understand you before
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Conrado Sanchez wrote:Here is the revised work. All I changed was the word empty to 0, which is what Mike. J. Thompson pointed out. Other than that I don't see anything that's wrong with it.
I worked hard and it seems I understood what you meant. But as Campbell Ritchie pointed out, it seems you mixing indexes and elements.

When you write A[0][1] or something similar, is a big chance that most of the people will understand that as an element which is present in array of arrays at indices 0 and 1. If I'm not mistaken, you're trying to show how elements looks inside array after you assign some values to a certain array positions. Mike was right, all array elements initially being assigned to 0's unless you explicitly assign them to a different values.

Probably less confusion would be if you would do your show in this way:
m[0] = 0; results array "m" elements to be [0, 0]
m[1] = 1; results array "m" elements to be [0, 1]
int[] p = m; results array "p" elements to be [0, 1]
p[0] = 4; results arrays "m", "p" elements to be [4, 1]
p[0] = 5; results arrays "m", "p" elements to be [5, 1]

Sorry I didn't understand you before


Yes!! that was what I was trying to say. Although I did notice I messed up the last one with p[0] = 5. Thanks!!!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that is what you would get if you print the array like this:-
System.out.println(Arrays.toString(myArray));
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!