Results 1 to 13 of 13
Thread: help with Array
- 02-11-2009, 10:28 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
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 #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Sort the array.
Java 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 #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 02-11-2009, 10:55 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
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 #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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 #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Did you read that page I've send? Logic is there in an example.
- 02-11-2009, 12:03 PM #7
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
yeah I did ,
but I didn't get it :( !
- 02-11-2009, 12:07 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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 #9
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
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 #10
Member
- Join Date
- Feb 2009
- Location
- Finland
- Posts
- 13
- Rep Power
- 0
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 #11
Member
- Join Date
- Feb 2009
- Posts
- 27
- Rep Power
- 0
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 #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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.
Java Code:f(arr[j] > arr[j + 1])
- 02-12-2009, 04:38 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Similar Threads
-
Array Reflection: Multi Array Reflection
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 09:08 PM
Bookmarks