Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Arrays and Collection

    Quote Originally Posted by wsaryada View Post

    Java Code:
    import java.util.Collections;
    Quote Originally Posted by wsaryada View Post
    Java Code:
      int min = (int) Collections.min(Arrays.asList(numbers));
    Hi,
    The codings work , and I tried a class with same concept but it shows errors ,
    Java Code:
    import java.util.Arrays;
    import java.util.Collections;
    public class ArrayMinMax 
    {
        public static void main(Integer[] args)
        {
            int min = (int) Collections.min(Arrays.asList(args));//the error is "Bound mismatch: The generic method min(Collection<? extends T>) 
    //of type Collections is not applicable for the arguments (List<int[]>). The inferred type int[] is not a valid substitute for the
    // bounded parameter <T extends Object & Comparable<? super T>>"
            int max = (int) Collections.max(Arrays.asList(args));
            System.out.println("Min number: " + min);
            System.out.println("Max number: " + max);
        }
    }
    Could u tell what should be done there..

    thx
    dhilip
    Last edited by noobplus; 03-21-2012 at 10:59 AM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Finding Max and Min values from string args

    It seems like the args passed to the Arrays.asList() method was treated like it was a primitive integers. What version of Java are you using?

    Btw, the main method of your java class should have the String[] as the parameter to make it executable.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Arrays and Collection

    Split from Finding Max and Min values from string args

    noobplus, don't hijack another poster's thread. If you do this again, your posts and any responses to them will be deleted.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Arrays and Collection

    Quote Originally Posted by DarrylBurke View Post
    Split from Finding Max and Min values from string args

    noobplus, don't hijack another poster's thread. If you do this again, your posts and any responses to them will be deleted.

    db
    I didn't hijack actually. I posted this in the old forum. Someone else moved it to a new post
    even I don't find an 'edit post' button in that post
    regards
    dhilip
    Last edited by noobplus; 03-22-2012 at 03:19 PM.

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Finding Max and Min values from string args

    Quote Originally Posted by wsaryada View Post
    It seems like the args passed to the Arrays.asList() method was treated like it was a primitive integers. What version of Java are you using?

    Btw, the main method of your java class should have the String[] as the parameter to make it executable.
    Hi,
    my java is java6, I did it in eclipse
    and I made "String[]" to "Integer[]" for the original starter of the thread 'mortus' because he wanted to find the maximum values of the arguments
    and line 5 containing "Integer[]" didn't show an error in eclipse with java 6
    thx,
    dhilip

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arrays and Collection

    w.r.t. the question: how can the minimum of a bunch of Strings ever be an int? Know what you're doing before you blindly try to copy a piece of code that you don't understand.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Arrays and Collection

    Quote Originally Posted by JosAH View Post
    w.r.t. the question: how can the minimum of a bunch of Strings ever be an int? Know what you're doing before you blindly try to copy a piece of code that you don't understand.

    Jos
    this was in an old thread
    he was asking to find maximum of the arguments given to a class
    and someone moved this post to a new thread by the time you were hearing to "clubbed to death" (not concerned if it is not from england and it is from hollywood anyways)
    Quote Originally Posted by JosAH View Post
    bunch of Strings
    bunch of Integer[] not Strings

    dhilip
    [code]

  8. #8
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Arrays and Collection

    Quote Originally Posted by JosAH View Post
    w.r.t. the question: how can the minimum of a bunch of Strings ever be an int? Know what you're doing before you blindly try to copy a piece of code that you don't understand.

    Jos
    Java Code:
    import java.util.Arrays;
    import java.util.Collections;
    public class ArrayMinMax
    {
        public static void main(String[] args)
        {
                 int intarray[] = new int[args.length];	     
    	    	 for(int i=0;i<intarray.length;i++)
    	    	 intarray[i] = Integer.parseInt(args[i]);
    	     int min = (int) Collections.min(Arrays.asList(intarray));//this one shows errors anyways too
            int max = (int) Collections.max(Arrays.asList(intarray));
            System.out.println("Min number: " + min);
            System.out.println("Max number: " + max);
        }
    }

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arrays and Collection

    Quote Originally Posted by noobplus View Post
    this was in an old thread
    he was asking to find maximum of the arguments given to a class
    and someone moved this post to a new thread by the time you were hearing to "clubbed to death" (not concerned if it is not from england and it is from hollywood anyways)

    bunch of Integer[] not Strings
    Not in your example; they're a bunch of Strings and the Collections.min( ... ) method tries to return a String; you can't weazle yourself out of it: your example was simply as wrong as can be; no excuses.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Arrays and Collection

    problem solved, this is the answer for Mortus
    Java Code:
    import java.util.Arrays;
    import java.util.Collections;
    public class ArrayMinMax
    {
        public static void main(String[] args)
        {
                 Integer intarray[] = new Integer[args.length];	     //I dont know difference between int[] and Integer[]
    // yet sorry for that
    	    	 for(int i=0;i<intarray.length;i++)
    	    	 intarray[i] = Integer.parseInt(args[i]);
    	     int min = (int) Collections.min(Arrays.asList(intarray));
            int max = (int) Collections.max(Arrays.asList(intarray));
            System.out.println("Min number: " + min);
            System.out.println("Max number: " + max);
        }
    }
    Last edited by noobplus; 03-22-2012 at 03:51 PM.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arrays and Collection

    Quote Originally Posted by noobplus View Post
    Java Code:
    import java.util.Arrays;
    import java.util.Collections;
    public class ArrayMinMax
    {
        public static void main(String[] args)
        {
                 int intarray[] = new int[args.length];	     
    	    	 for(int i=0;i<intarray.length;i++)
    	    	 intarray[i] = Integer.parseInt(args[i]);
    	     int min = (int) Collections.min(Arrays.asList(intarray));//this one shows errors anyways too
            int max = (int) Collections.max(Arrays.asList(intarray));
            System.out.println("Min number: " + min);
            System.out.println("Max number: " + max);
        }
    }
    See the API documentation for the Arrays.asList(T ... a) method; it doesn't understand an int[] as a bunch of ints; the work around is to use an Integer[] intarray instead; b.t.w. Eclipse would've told you so.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Arrays and Collection

    You should use Integer[] instead of int[] because the Arrays.asList() method required a parameter that implements the java.lang.Comparable.

  13. #13
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Arrays and Collection

    Quote Originally Posted by JosAH View Post
    See the API documentation for the Arrays.asList(T ... a) method; it doesn't understand an int[] as a bunch of ints; the work around is to use an Integer[] intarray instead; b.t.w. Eclipse would've told you so.

    Jos
    eclipse didn't ask me to convert it to Integer[] , i updated eclipse yesterday
    -edited: I mean it gave option only for "String[]"

    dhilip
    Last edited by noobplus; 03-22-2012 at 04:06 PM.

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arrays and Collection

    Quote Originally Posted by wsaryada View Post
    You should use Integer[] instead of int[] because the Arrays.asList() method required a parameter that implements the java.lang.Comparable.
    Where does the API documentation says so? The Array.asList( ... ) method just needs an array of objects (not primitives).
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arrays and Collection

    Quote Originally Posted by noobplus View Post
    eclipse didn't ask me to convert it to Integer[] , i updated eclipse yesterday
    -edited: I mean it gave option only for "String[]"
    Because the argument you supplied was a String[] in your original example (in this thread) don't obfuscate matters please with your incorrect remarks and apply Occam's razor ...

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Arrays and Collection

    Sorry, you were right Jos :) The method that require a parameter to implement the java.lang.Comparable is the Collections.min(...) / Collections.max(...) and not the Arrays.asList(...) method.

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arrays and Collection

    Quote Originally Posted by wsaryada View Post
    Sorry, you were right Jos :) The method that require a parameter to implement the java.lang.Comparable is the Collections.min(...) / Collections.max(...) and not the Arrays.asList(...) method.
    No need to say sorry; I just want to keep this thread as tidy as possible because the OP of this thread sprays around that much (irrelevant) nonsense that this thread can't be properly answered and closed anymore.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Arrays and Collection

    Quote Originally Posted by JosAH View Post
    No need to say sorry; I just want to keep this thread as tidy as possible because the OP of this thread sprays around that much (irrelevant) nonsense that this thread can't be properly answered and closed anymore.

    kind regards,

    Jos
    I didn't start the thread

  19. #19
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arrays and Collection

    Quote Originally Posted by noobplus View Post
    I didn't start the thread
    That is sophism: you hijacked another thread and a moderator split it and created a new thread (as you should've done).

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Arrays and Collection

    Quote Originally Posted by JosAH View Post
    That is sophism: you hijacked another thread and a moderator split it and created a new thread (as you should've done).

    Jos
    I was trying to help him. this code is cent percent on his topic. his name is Mortus. this code is trying to fine minimum of argument integer values. What is hijacking actually?

Page 1 of 2 12 LastLast

Similar Threads

  1. Collection
    By ShitalJain in forum New To Java
    Replies: 1
    Last Post: 06-25-2011, 10:56 AM
  2. Collection Help
    By spartan in forum New To Java
    Replies: 2
    Last Post: 03-18-2011, 09:18 AM
  3. Help me on Collection
    By kathir0301 in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 11:08 AM
  4. collection
    By D.Calladine in forum New To Java
    Replies: 1
    Last Post: 12-02-2010, 02:36 PM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Posting Permissions

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