Results 1 to 20 of 22
Thread: Arrays and Collection
- 03-21-2012, 11:53 AM #1
Arrays and Collection
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); } }
thx
dhilipLast edited by noobplus; 03-21-2012 at 11:59 AM.
- 03-22-2012, 03:15 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
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.Website: Learn Java by Examples
- 03-22-2012, 04:45 AM #3
Re: Arrays and Collection
Split from http://www.java-forums.org/new-java/...ring-args.html
noobplus, don't hijack another poster's thread. If you do this again, your posts and any responses to them will be deleted.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-22-2012, 04:05 PM #4
Re: Arrays and Collection
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
dhilipLast edited by noobplus; 03-22-2012 at 04:19 PM.
- 03-22-2012, 04:08 PM #5
Re: Finding Max and Min values from string args
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
- 03-22-2012, 04:13 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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.
JosBuild a wall around Donald Trump; I'll pay for it.
- 03-22-2012, 04:28 PM #7
Re: Arrays and Collection
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
dhilip
[code]
- 03-22-2012, 04:43 PM #8
Re: Arrays and Collection
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); } }
- 03-22-2012, 04:45 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Arrays and Collection
Build a wall around Donald Trump; I'll pay for it.
- 03-22-2012, 04:48 PM #10
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 04:51 PM.
- 03-22-2012, 04:49 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Arrays and Collection
Build a wall around Donald Trump; I'll pay for it.
- 03-22-2012, 04:55 PM #12
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
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.
Website: Learn Java by Examples
- 03-22-2012, 04:57 PM #13
Re: Arrays and Collection
Last edited by noobplus; 03-22-2012 at 05:06 PM.
- 03-22-2012, 05:17 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 03-22-2012, 05:19 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 03-22-2012, 05:48 PM #16
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
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.
Website: Learn Java by Examples
- 03-22-2012, 05:51 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Arrays and Collection
Build a wall around Donald Trump; I'll pay for it.
- 03-22-2012, 06:04 PM #18
Re: Arrays and Collection
- 03-22-2012, 06:21 PM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 03-22-2012, 06:30 PM #20
Re: Arrays and Collection
Similar Threads
-
Collection
By ShitalJain in forum New To JavaReplies: 1Last Post: 06-25-2011, 11:56 AM -
Collection Help
By spartan in forum New To JavaReplies: 2Last Post: 03-18-2011, 10:18 AM -
Help me on Collection
By kathir0301 in forum New To JavaReplies: 1Last Post: 12-03-2010, 12:08 PM -
collection
By D.Calladine in forum New To JavaReplies: 1Last Post: 12-02-2010, 03:36 PM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 02:40 AM
Bookmarks