• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

array index question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you guys, it's clear now.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Martin,

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

Thanks,
Gitesh
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic