Results 1 to 13 of 13

Thread: help with Array

  1. #1
    A.M.S is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default 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 , : )

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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]);
            }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    A.M.S is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default

    thanks Eranga ,
    but , how if I'd like to write the code by my self ? (without using Arrays.sort() )

    : )

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. :)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    A.M.S is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default

    yeah I did ,
    but I didn't get it :( !

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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?

  9. #9
    A.M.S is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default

    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,

  10. #10
    Zamppa is offline Member
    Join Date
    Feb 2009
    Location
    Finland
    Posts
    13
    Rep Power
    0

    Default

    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)

  11. #11
    chango77745 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    I am kinda new to java and I was confused about arrays also.

    This thread has helped me understand them much more.

    Thank you

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by A.M.S View Post
    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.

    Java Code:
    f(arr[j] > arr[j + 1])
    Give a try, if you have any question further I'll help you on this.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by chango77745 View Post
    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.

Similar Threads

  1. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •