Forums Register Login

Passing Array references

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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;
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
It works for me. When I run your program, I get:

+Pie Number of slices to send: Send
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.
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 887 times.
Similar Threads
arrayOutOfIndex
Problem in array length
What happens if the argument you want to pass is an object instead of a primitive?
Passing Elements of an array
for loop logic problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:02:38.