Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array index question

 
Martin Andersson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. class Comp2 {
2. public static void main(String[] args) {
3. float f1 = 2.3f;
4. float[][] f2 = {{42.Of}, {l.7f, 2.3f}, {2.6f, 2.7f}};
5. float[] f3 = {2.7f};
6. Long x = 42L;
7.
if (f3 == f2 [2] ) // f2[2] = ?

8. System.out.println("true");
9. }
10. }

Hello, what value are on index f2[2] = ? thanks for the help (42.Of}, {l.7f, 2.3f}, {2.6f, 2.7f)
 
Alpesh Vesuwala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Martin,

f2[2] will have memory address of {2.6f, 2.7f}

and in more details,



and for your question,



will compile fine but results in False.


Alpesh.
 
Hasnain Khan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



f3 is pointing to a 1 dimension array and f2[2] is pointing to a 2 dimension array i.e {2.6f, 2.7f}

f2[0] is pointing to {42.Of} 1D Array
f2[1] is pointing to {l.7f, 2.3f} 2D Array
f2[2] is pointing to {2.6f, 2.7f} 2D Array

so the if condition will evaluate to false.

Hope that helped.
Kind Regards.
Hasnain Javed Khan.
 
Martin Andersson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you guys, it's clear now.
 
Kishore Kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
float[][] f2 = {{42.Of}, {l.7f, 2.3f}, {2.6f, 2.7f}};

f2[0] is pointing to {42.Of} 1D Array of 1 element.
f2[1] is pointing to {l.7f, 2.3f} 1DArray of 2 elements.
f2[2] is pointing to {2.6f, 2.7f} 1D Array of 2 elements.

Hasnain Khan, i am correcting your sentences about the dimentions of the array.
 
Hasnain Khan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello kesava,
Thanks for correcting my mistake . I was very sleepy when I was answering the question . My Humblest Apologies for any confusions caused .
Kind regards.
Hasnain Javed Khan.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Martin,

I would just like to add that, "Array indexing starts from 0".

Thanks,
Gitesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic