• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array Confsion

 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
arraystuff.years[1][3] outputs 2.0 ?
 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
where is jls 4.3.1?
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here...
But you must promise never to use double arrays again, my son. They are evil...
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic