|
-
Mar 11th, 2003, 03:09 PM
#1
Thread Starter
Addicted Member
-
Mar 11th, 2003, 04:11 PM
#2
Dazed Member
Yeah i get the same results. The array being returned is unaffected by and changes made to it by any methods. Even when the array returned is assigned to another array.
Code:
class C {
public static void main(String args[]){
int[] i = {1,2,3,4,5};
for(int x = 0; x < i.length; ++x){
System.out.println(i[x]);
}
int[] i2 = squares(i);
for(int x = 0; x < i2.length; ++x){
System.out.println(i2[x]);
}
}
public static int[] squares(int[] i){
for(int x = 0; x < i.length; ++x){
System.out.println(i[x] * 2);
}
return i;
}
}
Last edited by Dilenger4; Mar 12th, 2003 at 12:11 AM.
-
Mar 11th, 2003, 04:16 PM
#3
Dazed Member
Here is what i dug up. If an actual parameter is a reference to an object, the the reference value of the object is passed. This means that bolth the actual parameter and the formal parameter are aliases to the same object during the invocation of the method. In particular, this implies that changes made to the object via the formal parameter will be apparent after the call returns but then they go on to say..... In summary, the formal parameter can only change the state of the object whose reference value was passed to the method. The parameter passing strategy in Java is call-by-value and not call-by-reference regardless of the type of parameter. These seem to be two contradicting statements.
Last edited by Dilenger4; Mar 11th, 2003 at 04:24 PM.
-
Mar 11th, 2003, 06:28 PM
#4
Thread Starter
Addicted Member
Yes they do, I fixed my immediate problem by making an temp array in fxn a that took b and then assigned it to array. This seemed to work, but I may be having another similar problem.
I'm coding a QuickSort and using Suns example: http://java.sun.com/applets/jdk/1.0/...Algorithm.java
Its not the same thing I guess you never pass more than 1 deep. So its not related to the reference chain.
Thanks for your help Dilenger4
NOMAD
-
Mar 12th, 2003, 01:19 AM
#5
Thread Starter
Addicted Member
Still no good, someone else have an answer? Tell me if Im on the right track.
Passing by reference passes an objects point so if I send it twice does it like not dereference it and sends the pointers pointer? It can't that would be counter intuitive and probably a compiler error.
I've search Sun with no more info that Dilenger4.
NOMAD
-
Mar 12th, 2003, 03:32 AM
#6
Thread Starter
Addicted Member
Code:
import java.lang.*;
public class ArrayTest
{
public static void main (String args[])
{
int [] mainArray = new int[10];
System.out.print("main(pre)::mainArray: ");
for (int j=0; j<mainArray.length; ++j)
System.out.print(mainArray[j] + " ");
System.out.println("");
fxnA( mainArray );
System.out.print("main(post)::mainArray: ");
for (int i=0; i<mainArray.length; ++i)
System.out.print(mainArray[i] + " ");
System.out.println("");
}
public static void fxnA( int [] arr )
{
System.out.print("fxnA(pre)::arr: ");
for (int i=0; i<arr.length; ++i)
System.out.print(arr[i] + " ");
System.out.println("");
for (int h=0; h<arr.length; ++h)
arr[h] = h;
fxnB( arr );
System.out.print("fxnA(post)::arr: ");
for (int j=0; j<arr.length; ++j)
System.out.print(arr[j] + " ");
System.out.println("");
}
public static void fxnB( int [] a )
{
System.out.print("fxnB(pre)::a: ");
for (int i=0; i<a.length; ++i)
System.out.print(a[i] + " ");
System.out.println("");
for (int h=0; h<a.length; ++h)
a[h] += a.length;
System.out.print("fxnB(post)::a: ");
for (int j=0; j<a.length; ++j)
System.out.print(a[j] + " ");
System.out.println("");
}
}
I found this to work, I'm confused. I guess there was something wrong in my code.
NOMAD
-
Mar 18th, 2003, 11:16 PM
#7
Originally posted by Dilenger4
Yeah i get the same results. The array being returned is unaffected by and changes made to it by any methods. Even when the array returned is assigned to another array.
Code:
class C {
public static void main(String args[]){
int[] i = {1,2,3,4,5};
for(int x = 0; x < i.length; ++x){
System.out.println(i[x]);
}
int[] i2 = squares(i);
for(int x = 0; x < i2.length; ++x){
System.out.println(i2[x]);
}
}
public static int[] squares(int[] i){
for(int x = 0; x < i.length; ++x){
System.out.println(i[x] * 2);
}
return i;
}
}
Code:
class C {
public static void main(String args[]){
int[] i = {1,2,3,4,5};
for(int x = 0; x < i.length; ++x){
System.out.println(i[x]);
}
int[] i2 = squares(i);
for(int x = 0; x < i2.length; ++x){
System.out.println(i2[x]);
}
}
public static int[] squares(int[] i){
for(int x = 0; x < i.length; ++x){
i[x] = i[x] * 2; //See this line. That has made the difference.
System.out.println(i[x] * 2);
}
return i;
}
}
I fixed your earlier example and the array is definitely getting changed. You were outputting the changes, but not assigning anything to the array.
Cheers!
Abhijit
-
Mar 18th, 2003, 11:57 PM
#8
Dazed Member
Ahh ok. I thought i was starting to go crazy.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|