aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array doubt" Watch "array doubt" New topic
Author

array doubt

cathymala louis
Ranch Hand

Joined: Nov 02, 2005
Posts: 77
This is a mock exam question. The answer given is: 0 0 3 0
Could some one please explain how we will get 3 in arr[2].

Lata Bagga
Ranch Hand

Joined: Jun 13, 2007
Posts: 35
Hello Cathymala,

Earlier I had the same doubt in this question but I got this explanation from Vishwanath which cleared my doubt and hope will clear yours also:

for the first value in the array:
arr[1]<-- 0
so arr becomes,
{1,0,3,4}

for the next value in the array: [Remb: The array is changed now]

arr[0]<-- 0;
and arr is now,
{0,0,3,4}

for the next value in the array:

arr[3]<--0
gives
{0,0,3,0}

and for the last value in the array:
arr[0]<--0
so, arr remains {0,0,3,0}


Regards,
Lata
Akshay Dashrath
Greenhorn

Joined: Jan 10, 2008
Posts: 11
I could not understand this explanation, why do you start with arr[1] when its actually goes arr[0], arr[1], arr[2] etc.. and why does it remain 3 only in the 3rd position.

Thank you in advance,

Akshay
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The zeroth element in the array has a value of 1 which is what gets assigned to i in the first iteration.

arr[i] equals array[1].


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Akshay Dashrath
Greenhorn

Joined: Jan 10, 2008
Posts: 11
Ben,

Is'nt it like this now, does'nt an array always start from the zeroeth element?

a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4

Thanks,

Akshay
ramchandra sugasi
Greenhorn

Joined: Jan 16, 2008
Posts: 10
hi,

the critical thing to notice here is that

'i' is used to hold the value in the array
and also to index through the array.

Hence,

when we go step by step into the for loop, its as follows:
arr = {1,2,3,4}

loop(1)
i = 1 , arr[1] = 0 // i takes first value of the array arr
arr = {1,0,3,4}

loop(2)
i = 0, arr[0] = 0 // i takes second value of the array arr
arr = {0,0,3,4}

loop(3)
i = 3 , arr[3] = 0 // i takes third value of the array arr
arr = {0,0,3,0}

loop(4)
i = 0 , arr[0] = 0 // i takes fourth value of the array arr
arr = {0,0,3,0}

confusing...
but observe things step by step and it will be clear.
cathymala louis
Ranch Hand

Joined: Nov 02, 2005
Posts: 77
Thank you, I got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array doubt