aspose file tools*
The moose likes Beginning Java and the fly likes Array elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array elements" Watch "Array elements" New topic
Author

Array elements

J. Moritz
Greenhorn

Joined: Nov 18, 2004
Posts: 9
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

Joined: Jul 30, 2003
Posts: 805
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.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
despite how clever "coffee man java man" is


I've heard it takes forever to grow a woman from the ground
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Nov 18, 2004
Posts: 9
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

Joined: Apr 04, 2004
Posts: 815
Naming Policy

Do I get to be a bartender now?
J. Moritz
Greenhorn

Joined: Nov 18, 2004
Posts: 9
Better?
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Hey, Nick -

If you're a bartender, give us a Heineken, okay?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
J. Moritz
Greenhorn

Joined: Nov 18, 2004
Posts: 9
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

Joined: Mar 13, 2004
Posts: 1272
How about pseudocode?


Now you just have to code this in Java.


Mike Gershman
SCJP 1.4, SCWCD in process
J. Moritz
Greenhorn

Joined: Nov 18, 2004
Posts: 9
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

Joined: Oct 02, 2003
Posts: 11479
    
  16

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;


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
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."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array elements