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 Passing Elements of an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing Elements of an array" Watch "Passing Elements of an array" New topic
Author

Passing Elements of an array

KN
Ranch Hand

Joined: Aug 13, 2000
Posts: 66
Hello everybody,
I have a doubt in this question. When elements of an array are of type data primitive and are passed to a method then any changes to their values must not be reflected outside that method. But here the value is changed. Can anybody explain me what is happening in this code. I think the output should be
minvalue = 8(i.e. not changed) whereas the actual output is
minvalue = 1.
Thanks


[This message has been edited by Jim Yingst (edited August 04, 2000).]
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1850
Hi,
The code does not compile. Please try and make it easy for others. If you do not do so others will not be able to answer you .
Regds.
Rahul
KN
Ranch Hand

Joined: Aug 13, 2000
Posts: 66
Hi Rahul,
There is some problem with Javaranch editor. Sometimes it skips some part of the code. This is third time it has happened with me. I am not sure what the problem is. Now I am again sending the correct code. Hope this time it will post correctly.
Sorry for the trouble.
Thanks
class FindMinAr{
public static void main(String [] args){
int dataseq []= {8,4,6,2,1};
int minValue = dataseq[0];
for(int index =1;index<dataseq.length; ++index)>
minValue= minimum(minValue,dataseq[index]);
System.out.println("Minimum Value: " + minValue);
}
public static int minimum(int i, int j){
return (i<=j)?i:j;
}
}
KN
Ranch Hand

Joined: Aug 13, 2000
Posts: 66
It has happened again. I am posting it again. Please bear with me.
class FindMinAr{
public static void main(String [] args){
int dataseq []= {8,4,6,2,1};
int minValue = dataseq[0];
for(int index =1;index<dataseq.length; ++index)>
minValue= minimum(minValue,dataseq[index]);
System.out.println("Minimum Value: " + minValue);
}
public static int minimum(int i, int j){
return (i<=j)?i:j;
}
}
KN
Ranch Hand

Joined: Aug 13, 2000
Posts: 66
Oh! not again! Hope it works fine this time.
class FindMinAr{
public static void main(String [] args){
int dataseq [] = {8,4,6,2,1};
int minValue = dataseq[0];
for(int index = 1; index<dataseq.length;++index)>
minValue = minimum(minValue, dataseq[index]);
System.out.println("Minimum Value: " + minValue);
}
public static int minimum(int i, int j){
return (i<=j)?i:j;
}
}
KN
Ranch Hand

Joined: Aug 13, 2000
Posts: 66
Thanks a lot, Jim. I am trying to post it correctly but it's not working. Can you please tell me why it's happening & how to correct it. Can you please clear my doubt also. Why the output is 1, instead of 8.
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1850
Hi ,
here is your code modified a little

which prints out
Before 8, 4 and
4,4
after method minimum is invoked
the method minimum returns
return (i <= j) ? i : j;
in this case 8<=4? 8: 4
since 8<=4 is false it returns 4 which is correct
Hope things stand clarified
Regds.
Rahul
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi KN,
What makes you think that the for loop will terminate after the first iteration ???
In your case, the for loop is iterating over all the elements and the last element is 1. Your minimum method will return 1 obviously as it is smaller than the zeroth element.
Hope it is clear . . .
Regards,
Shashank
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
KN,
Your sample code has this functionality. It takes an array of integer primitives and finds out the minimum interger value among all array elements. Let see what's happening.

<pre>

class FindMinAr{
public static void main(String [] args){
//dataseq is an array of integers
int dataseq []= {8,4,6,2,1};

/* You assign the 1st array element value to another 'int' variable.
So minValue = 8; here */
int minValue = dataseq[0];

/*Here you loop for 1,2,3,4 times, since index value starts with 1 and
iterates upto < arraylength, which is 4 So totally 4 times the loop iterates. */
for (int index = 1; index < dataseq.length; ++index) {
</pre>
/*
Please note here carefully. In java for any method we pass values as parameters.Which means what we pass as arguments to a method comes back as it is. So what you interpreted is correct.
When we pass an array element alone, for example dataseq[0] / dataseq[1] / dataseq[2]... dataseq[4] as an argument to a method, and after the method's execution, if we try to print out the array elments ,dataseq[0] / dataseq[1] / dataseq[2]... dataseq[4], their value WILL NOT BE CHANGED. Since we just pass the value of the array elements. So another copy of this array element is what exactly sent inside any method. So whatever you do inside the method is not dealing with the original copy at all.
So having said this part, in your code what's happening inside the minimum(...) method ? You are just comparing the 2 inputs and return the min argument. Isn't? So if you see the following code carefully, for every iteration, you send the minvalue and the array element value for that iteration and get the minimum in turn and assign back to minValue variable.
So this is what really happens.

Loop1 :
minvalue = 8;
minvalue = minimum(8,4); (since we start from 1st array element I put 4 as the 2nd argument for this function.)
minvalue = 4;
Loop2 :
minvalue = 4;
minvalue = minimum(4,6); //compare with array element 2
minvalue = 4;
Loop3 :
minvalue = 4;
minvalue = minimum(4,2); // compare with array element 3
minvalue = 2;
Loop4 :
minvalue = 2;
minvalue = minimum(2,1); // compare with array element 4
minvalue = 1;

So finally we got minValue as 1, since every time we reassign the int variable ( NOT ARRAY ELEMENT ), with the result of the minimum(..) method. */
<pre>

minValue= minimum(minValue,dataseq[index]); }
System.out.println("Minimum Value: " + minValue);
}
public static int minimum(int i, int j){
return (i <= j) ? i : j;
}
}

</pre>

Do you get the point KN?. We are NOT CHANGING THE ARRAY ELEMENT inside the minimum(...) method at all.
We just compare the 2 'int' arguments and return the minimum value. Also the point you are missing is (I think ), the local 'int' variable minValue is JUST AN ANOTHER int variable and NOT RELATED TO array elements. It is NOT an array element. The minValue is a local int variable which just happened to have the 0th element of array dataSeq. (dataSeq[0]) as its initial value. This DOES NOT MEAN , the local int var should not change its value. The minValue var can very well take any value and change its value. Also you need to understand the logic of the code. Every time we compare the present minValue var's value with the iterated array element and among the 2 values , whichever is minimum is ASSIGNED BACK TO minValue var. THis is the logic and according to this logic we get the correct result. So among all the array elements we get '1' as the min value which is logically correct.

To also prove your point, which is 'when we send array elements as value to ant method, after the method returns the original array elements values are not changed' , try to print out ALL THE ARRAY ELEMENTS AGAIN. In other words add this following code.


Output :
8
4
6
2
1

See. ALL ARRAY ELEMENTS are NOT CHANGED. I think you misunderstood the local varible 'minValue' as one of array elements. Is this somewhat clear to you now KN?
I almost forgot to tell. To get the sample code shown as it is with the tabs etc use [ CODE ] sample code here [ /CODE ] tags. There SHOULD NOT be any space(s) inside the brackets[ ] which contain the CODE and /CODE. I purpusly introduced here in order to not to be interpreted by this discussion software.
Also when we write the < char , for example for(int i=o; i < 10; i++) try to put a space char after the < char. Otherwise the '<' char along with the other words are considered as some meaningful tags by this s/w. So always put spaces before and after the '<' char. Then everything will be fine.
regds
maha anna.

[This message has been edited by maha anna (edited August 05, 2000).]
KN
Ranch Hand

Joined: Aug 13, 2000
Posts: 66
Thanks everybody for trying to solve my problem especially Maha anna who understood what exactly I was asking. You are right I was confusing the variable minValue with an array element & I thought that this code is changing the elements of the given array. Now I know that array is not changed at all. Thanks Maha for the GREAT EXPLAINATION! Also, I visited your new home. It's really useful for people preparing for SCJP.
I also understood what was the problem when I was posting my code. I will take care next time.
Regards,
Kiran.
 
jQuery in Action, 2nd edition
 
subject: Passing Elements of an array