aspose file tools*
The moose likes Beginning Java and the fly likes Passing Array references Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Array references" Watch "Passing Array references" New topic
Author

Passing Array references

prerna boja
Ranch Hand

Joined: Aug 19, 2004
Posts: 67
Hi all,

I have a problem with the code.



at line 1(ie as indicated ------------------->(1) ) ,I can never reach table[i] =table[j] ,but still the code executes and if possible plz expalin how the code works. I tried but i am very confused. Plz explain me passing array refernces ,if possible or let me know where can i chekc it out in detail.

[ EJFH: Added UBB Code tags ]
[ January 07, 2005: Message edited by: Ernest Friedman-Hill ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

What do you mean when you say that you "can never reach" the line marked "1"?

One good place to learn about passing references as function arguments is our "Campfire Stories" -- in particular, here and here.


[Jess in Action][AskingGoodQuestions]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I copied and pasted your code into eclipse and it ran fine. The method

public static swap(int, int)

is never called, but the method

public static swap(int[], int, int)

is run.

Are you getting errors? It looks like one pass through a bubble sort.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

You can also look at the Sun's Tutorial on Arrays. That may also help you.

I also echo Ernest's question as to what your question is since your code does reach line 1. I suspect that the issue is that it is not doing what you think it should do.
prerna boja
Ranch Hand

Joined: Aug 19, 2004
Posts: 67
Hi,

I mean to say that

see when the method public static void swap(int[] table,int i,int j)
is called it does all the calculations in the method.

i mean when it is called first
int tmp= table[0] (ie index-1 = int i ,so i =0)=>tmp = 6 (as table[0]=dataSeq[0])

then


table[i] = table[j] => table[0]=table[1] => 6=4.

(in this way it goes but it is never equal so how can it reach the nxt statement then again go back to the for loop n start the process again by increamenting the index.)

then

table[j]=tmp; =>table[1]=6;
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Ah.

table[j] = table[i];

is an instruction, not a test. It means that Java should take the value in table[i], and copy it into table[j]. The "=" operator always means this. The "==" operator -- two equals signs together -- is a test for equality.
prerna boja
Ranch Hand

Joined: Aug 19, 2004
Posts: 67
Hi,

I got it .Thanks for the reply.

But I got stuck up again when I started to work out i get

the output as


dataSeq[1] =6
dataSeq[3] =8
dataSeq[4] =2

but never able to reach dataSeq[0] and dataSeq[2]


Thanks Once again to help me out.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
It works for me. When I run your program, I get:



Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I'm not sure what you did. The first code you posted executes exactly as it should.

To me it looks like the first pass of a bubble sort. If it is you would need to add another for loop around the first one to loop the lenght of the array.

If not it would help if you were to explain what you are trying to do.
 
jQuery in Action, 2nd edition
 
subject: Passing Array references