my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Confsion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array Confsion " Watch "Array Confsion " New topic
Author

Array Confsion

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
I modify this code just a bit...

This code produces the result:
Joe Sample 2.0
Smooth Jazz 2.0
This baffles me because i thought the code will produce an error(java.lang.ArrayIndexOutOfBoundsException)
with regards to this line
coz i thought the array months can only handle indexes 0 - 2. Anyway I experimented on the code itself...i removed this line... months = years[1];
It produces the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
My question is what does this line of code do? DId this increase the capacity of the array variable months??? Help! im confused!


Be Afraid...Be very Afraid...
Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41
Look the line:
months = years[1];
-----------0-1-2-3
years[0] 0
years[1] 1-0-0-0-2.0 //months point here
years[2] 2
months is years[1]'s reference.
hence,
months[3] == years[1][3] == 2.0
[ July 25, 2002: Message edited by: Val Lee ]
[ July 25, 2002: Message edited by: Val Lee ]

Val SCJP2 1.2<br />====>SCJD
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
good
confusing question
but I think Val has explained it.
when you say :
months = years[1];
monthsh is now pointing to array of 4 of type float because year is 2-D array 3,4. so now months can take 3 value.
Try to access 4th index of months OR GT 4


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
arraystuff.years[1][3] outputs 2.0 ?


SCJP 1.4
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
i think i found the answer to my question at JLS 4.3.1:
If two variables contain references to the same object, the state of the object can be modified using one variable's reference to the object, and then the altered state can be observed through the reference in the other variable.
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
where is jls 4.3.1?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
here...
But you must promise never to use double arrays again, my son. They are evil...
Jonathas Carrijo
Greenhorn

Joined: Jul 25, 2002
Posts: 25
Well...
I'm afraid I'm just repeating everything but... I actually wanna figure out if I got it right!
First of all: Is an array an object??? Please answer, I'm new to java...
Well...
Let us take a look back at declaration of 'years' and 'months':
float years[] [];
float months[];
So now months is a variable in which u can allocate any array of floats.
Now, let's jump to the code line in question:
months = years[1];
As I understand, if stuff produced in the right of the '=' operator is of the same kind of that the variable in the left of '=' is prepared to contain, then the assignment will, with no problems, be executed.
And that is our case! 'years' is a matrix, and 'years[1]' refers to an array of floats corresponding to the second vector or the 'years' matrix (the first vector is 'years[0]'). And months is prepared to contain such kind of stuff.
Notice, by the following line:
years = new float[3] [4];
that years is a matrix with THREE vectors of FOUR elements each. So, the array passed to 'months' is of 4 elements.
This explains how 'months[3]' doesn't generate any Array...Exception!
Well... think I'm done!
Ah! And please answer to my question! ('Is an array an object?)
And... please, forgive my english, for I'm native brazilian =)
Luck to y'all!


Java has reached Brazil =D
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Yes that is correct.
(Although I feel uncomfortable with the use of "matrix" since I feel it's really an "array of arrays", and an array is not a Vector, strictly speaking, but then that's just me )
Yes an array is an Object. See JLS 10.2.
Jonathas Carrijo
Greenhorn

Joined: Jul 25, 2002
Posts: 25
Oh, sorry! Probably some cultural or english/portuguese discrepations
Actually i often get confused about math and programming concepts, cause I'm studying linear algebra at univ.
Thanx for your answer too!
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Java has a Vector class, but it doesn't correspond to the usual mathematical vector. It's more like a dynamic array.
It would be fun to implement such a Vector class though...
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Well, I had some time off so might as well play around with it...
Here's a crude math.Vector class that represents a real-valued vector. As you can see, it still lacks the cross product and scalar multiplication operations required.
(I'm still thinking about using a general Scalar class or just use doubles)
Also, the methods that implement the usual vector algebra is not as syntactically elegant as it would have been in C++, but I can live with it.


After finishing up this Vector class, it would be nice to move on to a 3x3 Matrix class...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Array Confsion