Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array elements

 
J. Moritz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi java ranchers,

Is there a method that will help me to arrange the elements (primitive integer elements) of an array so that the lowest value is at array index 0 and the highest value is at the last index of the array?
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure is. And if you change your display name to match JavaRanch's naming policy, I'm sure lots of us would be glad to tell you about them.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
despite how clever "coffee man java man" is
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by coffee man java man:
Hi java ranchers,

Is there a method that will help me to arrange the elements (primitive integer elements) of an array so that the lowest value is at array index 0 and the highest value is at the last index of the array?




Read the java.util.Arrays API for more info.
 
J. Moritz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your responses? How am I breaking policy with this name? I'll change it but I was not aware I was breaking a policy of this forum.

Thank you Joyce for your answer. That's what I was looking for.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naming Policy

Do I get to be a bartender now?
 
J. Moritz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better?
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Nick -

If you're a bartender, give us a Heineken, okay?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nick George:

Do I get to be a bartender now?


Good work. You can be a deputy busboy. Now do that a coupla hunnert times, you'll be all set.
 
J. Moritz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

The sort method of the Arrays class works perfectly, but what I need is a way to find the minimum and maximum values of an array and print these values onscreen with their corresponding index number (non-sorted index numbers).

Thanks
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about pseudocode?


Now you just have to code this in Java.
 
J. Moritz
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike...thanks for taking the time to explain.

Coding it is the hard part. I haven't tried your way yet. I've been using nested for loops to take the last indices and run each other indices against it once the other indices get incremented in the outside loop. I keep getting out of bounds errors that way.

I'll try your way.. looks like this one is gonna be another all-nighter! coffee time!

Thanks man!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This shouldn't be too hard, if you code correctly. what i mean by that is this:

Wrong way to write code:
write the entire program
compile it
freak out when you find 100 compile errors
fix all the compile errors at once
freak out when there are 98 compile errors
...
compile
freak out when it doesn't work
try to fix everything
freak out when you get 52 compile errors...

CORRECT way to write code:
Write as little code as possible (to start with, just a class with a main method that does nothing)
compile it
fix compile errors (shouldn't be too many if you've only written 6 lines)
write code to create array - again, should only be a few lines at most
get it to compile.
maybe put in some lines to print out your array - to verify the array is being created correctly. note that you may take this code out for your final program.
keep adding the fewest lines possible to complete the next step. then compile-test-fix each few lines.

the nice thing about psuedo-code is that it's almost a line for line translation to java code. if you can't change one line of pseudocode to one line of java, break it down into smaller steps.

as you get more experience, you won't have to break it down quite as far each time.

take this line:
save first array element with its index as the smallest element

that's a little much for one line of java, so simplify it to:

save first array element (since it has to be the smallest so far)
save the index of that position (since it will be the index of the smallest found so far)

aha!!! these two lines sound like one line of java each:
int smallestValueFound = myArray[0];
int indexSmallestFound = 0;
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CORRECT way to write code:
Write as little code as possible (to start with, just a class with a main method that does nothing) compile it fix compile errors (shouldn't be too many if you've only written 6 lines)
write code to create array - again, should only be a few lines at most
get it to compile.
maybe put in some lines to print out your array - to verify the array is being created correctly. note that you may take this code out for your final program.
keep adding the fewest lines possible to complete the next step. then compile-test-fix each few lines.


I'd like to add a first step: Write out (on paper or in a word processor) your design and make sure it works "on paper".

As the old addage goes, "Measure twice, cut once."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic