# help with Array

• 02-11-2009, 10:28 AM
A.M.S
help with Array
Hi everyone ,

I'll have a java exam tomorrow , it'll be about the arrays ..

But I have a problem that :
If I have an array (int) its size : 10 like this :
{4,2,1,6,5,8,10,9,3,7}
how may I make it like this :
{1,2,3,4,5,6,7,8,9,10} (from the small to the large)

I think it's really easy but I couldn't make it :( !

thanks , : )
• 02-11-2009, 10:40 AM
Eranga
Sort the array.

Code:

```        int[] arr = {4,2,1,6,5,8,10,9,3,7};         java.util.Arrays.sort(arr);         for(int i = 0; i < arr.length; i++) {             System.out.println(arr[i]);         }```
• 02-11-2009, 10:41 AM
Eranga
See what's the result on the above code.

May be this can be helpful to you. :)
• 02-11-2009, 10:55 AM
A.M.S
thanks Eranga ,
but , how if I'd like to write the code by my self ? (without using Arrays.sort() )

: )
• 02-11-2009, 11:09 AM
Eranga
Get an index and search it through all the other elements, compare for the minimum. Then added values into another array.

I don't know what the point avoiding standard APIs. :)
• 02-11-2009, 11:17 AM
Eranga
Did you read that page I've send? Logic is there in an example.
• 02-11-2009, 12:03 PM
A.M.S
yeah I did ,
but I didn't get it :( !
• 02-11-2009, 12:07 PM
Eranga
Did you read that String array sorting, 2nd example of that page. Just you have to do few changes. Can you show what you have tried so far?
• 02-11-2009, 12:27 PM
A.M.S
int [] Arr = {5,2,3,6,1};

int x = Arr.length - 1;
int c = 0;
for (int i = 0 ; i < x ; i ++)
for (int j = 0 ; j < x ; j ++)
if (Arr[j].compareTo(Arr[j+1]) > 0)
{
c = Arr[j];
Arr[j] = Arr[j+1];
Arr[j+1] = c;
}

this's what I've tried , it gave me error with this :
if (Arr[j].compareTo(Arr[j+1]) > 0)
by the way , this is the first time I see .compareTo , what's its work ?

thanks again,
• 02-11-2009, 02:36 PM
Zamppa
compareTo is for Strings. Since you are working with primitives you can simply use the < and > operators.

In other words, replace
(X.compareTo(Y) > 0 )
with something like
(X > Y)
• 02-11-2009, 06:06 PM
chango77745
I am kinda new to java and I was confused about arrays also.

This thread has helped me understand them much more.

Thank you
• 02-12-2009, 04:36 AM
Eranga
Quote:

Originally Posted by A.M.S
it gave me error with this :
if (Arr[j].compareTo(Arr[j+1]) > 0)
by the way , this is the first time I see .compareTo , what's its work ?

thanks again,

Yes this is wrong. Did you read the Java doc about compareTo()? It's all about strings, not deal with int arrays. Simply you want to compare elements. == make sense there.

You can do something like this.

Code:

`f(arr[j] > arr[j + 1])`
Give a try, if you have any question further I'll help you on this.
• 02-12-2009, 04:38 AM
Eranga
Quote:

Originally Posted by chango77745
I am kinda new to java and I was confused about arrays also.

This thread has helped me understand them much more.

Thank you

Looking into others thread make real sense lol. Keep going. At the same time if you have question related to arrays ask your question more clearly. If your question is not directly related to this thread please start a new one.