• 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

Comparison of arrays

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sierra/Bates, Chapter 4, Question 2

Given:



And the following five code fragments:

F1. if(f1 == f2)
F2. if(f1 == f2[2][1])
F3. if(x == f2[0][0])
F4. if(f1 == f2[1,1])
F5. if(f3 == f2[2])

I can understand that F2 and F3 compiles, and that F3 returns true. But why does F5 compile? Isn't f2 a 2-dimensional array?
And what does f2[2] even refer to? I would think the proper way to refer to an element in f2 would be f2[index 1][index 2].

Please clarify
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:

And the following five code fragments:

F1. if(f1 == f2)
F2. if(f1 == f2[2][1])
F3. if(x == f2[0][0])
F4. if(f1 == f2[1,1])
F5. if(f3 == f2[2])

I can understand that F2 and F3 compiles, and that F3 returns true. But why does F5 compile? Isn't f2 a 2-dimensional array?
And what does f2[2] even refer to? I would think the proper way to refer to an element in f2 would be f2[index 1][index 2].

Please clarify



The answer : F3,

And for compiling the option, you are comparing two, one dimensional arrays. Yea, f2 is a two dimensional array. f2[2] refers a one dimensional array(in your case, {2.6f, 2.7f}).

Sandra Bachan wrote:...would be f2[index 1][index 2]


This is for mat lab, not for java. In java multi dimensional arrays are arrays of array.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if I understand correctly:

if (2.7f == {2.6f,2.7f})


is considered correct syntax, and that is why it is compiling and running?

I am really confused.......
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:So if I understand correctly:

if (2.7f == {2.6f,2.7f})




Which option you are talking? F5? If so, your f3 is a array, not a float value!
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll clarify my question:

According to Sierra/Bates, and according to my own hands-on coding, the following compiles and runs




Please explain why this program compiles and runs? According to me, this program should not compile because


f2[2]

is the incorrect syntax for referring to a 2-dimensional array. If it would have been

f2[2][1]

then I can understand, because this refers to a single element in the 2-dimensional array, and

if(f3 == f2[2][1])


would make sense because you are comparing a single element with another single element

Please guide.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
f2 is a two dimensional array. Please go through the Chapter 3 in K&B's book. You'll definitely get this, in that, they explained with the help of figures.

And, arrays of arrays means, a array having reference to another array, so two dimensional array f2 has three(in your case) references to primitive int array which are {42.0f},{1.7f,2.3f},{2.6f,2.7f} respectively.

Got it?
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
f2 is a two dimensional array...

f[0],f[1] and f[2] refers to the one-dimensional array of 2 elements...

f3 is 1-D Array...

So, comparing f3 1-D and f2[2] which refers to 1-D array is legal and will be compiled...
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ram Narayan.M wrote:f2 is a two dimensional array...

f[0],f[1] and f[2] refers to the one-dimensional array of 2 elements...




OK, this part I understand.

This is really interesting, I never saw anything like this. I don't recall seeing this type of concept in Sierra/Bates, but conceptually, it make sense, because you are comparing 1-d array with 1-d array. I re-wrote the code to output f3 and f2[2], I get memory references


I created code to clear this concept:




Output:


Is f3 == f2[2]?
false
f3 is [F@1bc4459
f2[2] is [F@12b6651
Assign f2[2] to f3
Is f3 == f2[2]?
true
f3 is [F@12b6651
f2[2] is [F@12b6651


 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:

OK, this part I understand.

This is really interesting, I never saw anything like this. I don't recall seeing this type of concept in Sierra/Bates, but conceptually, it make sense, because you are comparing 1-d array with 1-d array. I re-wrote the code to output f3 and f2[2], I get memory references


I created code to clear this concept:



Kathy/Bates DO explained this in chapter 3. Check it, they've explained with figures.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You also have to stop thinking of it as "two-dimensional" arrays -- as technically, Java doesn't really support them. It is technically, an array of arrays. And in that regard, it makes sense, as when you dereference an array of arrays, you get an array.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic