Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Array references

 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works for me. When I run your program, I get:

 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic