Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

simple array

  Asked By: Molly    Date: Nov 16    Category: Java    Views: 614
  

i have a class that takes a user input of how many integers and then
takes in the same number of intergers. What I need to do is create
some sort of array and run throgh the input to get the smallest and
the largest etc. Here's code:

import java.util.ArrayList;

public class SelectionSort
{
public static void main( String[] args )
{

Terminal terminal = new Terminal();
int sortTotal;

sortTotal = terminal.readInt("Integers to sort: ");

int number[] = new int[sortTotal];

for (int i = 0; i < number.length; i++) {

int n = terminal.readInt("> ");



}


}

}

The output is:

Integers to sort: 3
> 2
> 4
> 6

can anybody get me started with a sort method.

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Baylen Smith     Answered On: Nov 16

> can anybody get me started  with a sort  method.

That's an easy one.

import java.util.Arrays;
...
Arrays.sort(numbers);

 
Answer #2    Answered By: Lughaidh Fischer     Answered On: Nov 16

there are several sorting methods, I suggest you to start with the
simplest, just named "Simple Sort".
You can find explanation, source and a nice demo here
www.cs.brockport.edu/.../simplesort.html

 
Answer #3    Answered By: Aalia Arain     Answered On: Nov 16

Yes this confirms what I was looking for
which was a nested for loop. Here is my code:

int[] numbers = new int[sortTotal];

for ( index = 0; index < numbers.length; index++) {

numbers[index] = terminal.readInt(">");
}

for (index = 0; index < numbers.length; index++){

min = index;
for ( j = index+1; j < numbers.length; j++){
if ( numbers[j] < numbers[min] )
min = j;
}

temp = numbers[index];
numbers[index] = numbers[min];
numbers[min] = temp;

}

 
Answer #4    Answered By: Terence Mitchell     Answered On: Nov 16

Try to use java  Collections. They are all so usefull.

 
Didn't find what you were looking for? Find more on simple array Or get search suggestion and latest updates.




Tagged: