File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparison of arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Comparison of arrays" Watch "Comparison of arrays" New topic
Author

Comparison of arrays

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Feb 18, 2010
Posts: 434
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

Joined: Nov 04, 2009
Posts: 2066

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?
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

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...


SCJP 6 [SCJP - Old is Gold]
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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

Joined: Nov 04, 2009
Posts: 2066

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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparison of arrays