Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

resorting array  RSS feed

 
Kenneth Van Gysegem
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so the idea is to find an specific numer in a given array, change it to a zero and change the index so it is the last element in the array.
All the other elements should then move so their index = index -1
So this is what I've got so far:


This changes the numbers for instance 3 into 0 and puts them in the back of the array but the output is wrong
Output:
4, 8, 2, 5, 5, 17, 7, 99, 0, 0

I can't seem to find the problem though. Can anybody help or give a hint where I am going wrong here?
Thanks in advance.
 
Knute Snortum
Sheriff
Posts: 4079
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you using "j" in the inner for-loop?
 
Kenneth Van Gysegem
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I kind of just used it to loop over all the elements in the array starting vrom element at index position.
 
Knute Snortum
Sheriff
Posts: 4079
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you aren't using "j" as an index for array1 inside the inner for-loop. Think about what actually gets assigned in that loop.
 
Kenneth Van Gysegem
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks, I see where I go wrong. I'm not sure how I'm going to solve the problem yet.
But I'll try a bit more and keep you posted.
Thanks again!
 
Kenneth Van Gysegem
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey again, I found the solution.



Thanks again.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!