Array Of Varying Size

Hi

I need to create an array of user defined length. Basically i want to allow the user to enter the length in an dialog box or similar and then the array will be created of that size.

The array will then be filled with randomly generated numbers.

Then i need the array to be sorted quickly.

I CAN THEN APPLY THE BINARY SEARCH!

Can anyone help with the coding for the above task please. My actual binary search coding is fine its just the creating the array and so on that im having difficulties with.

Thnx in advance

[560 byte] By [Princea] at [2007-10-2 5:33:16]
# 1

int n = Integer.parseInt(...); // input from user

if (n < 0)

throw new RuntimeException("value >= 0 required");

int[] arr = new int[n];

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

arr[i] = ... Math.random() * ... ;

java.util.Arrays.sort(arr);

// do binary search

rkippena at 2007-7-16 1:44:05 > top of Java-index,Other Topics,Algorithms...
# 2

public class Binary_Search

{

int n = Integer.parseInt(...); // input from user

if (n < 0)

throw new RuntimeException("value >= 0 required");

int[] arr = new int[n];

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

arr = ... Math.random() * ... ;

java.util.Arrays.sort(arr);

public int binarySearch(int arr[],int key)

{

int low=0;

int high=arr.length-1;

while(low<=high)

{

int middle=low + (high - low)/2;

if(key==arr[middle])

return middle;

else

if(key<arr[middle]) high=middle-1;

else low=middle+1;

}

return -1;//not found

}}

Would that be the full code mate?>

Princea at 2007-7-16 1:44:05 > top of Java-index,Other Topics,Algorithms...
# 3
Is the above code i copied correct because when i compile it i get errors. Can you please put the code together properly please m8Thnx
Princea at 2007-7-16 1:44:05 > top of Java-index,Other Topics,Algorithms...
# 4
> Is the above code i copied correct because when i> compile it i get errors.No, I intentionally posted bad code so that you would learnhow to fix it.> Can you please put the code> together properly please m8No.
rkippena at 2007-7-16 1:44:05 > top of Java-index,Other Topics,Algorithms...