• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing Array references

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic