Collections.max(array) is Not Working - Java -
i using collections.max(array); determine largest number in selected array is. when use it, error message:
exception in thread "main" java.lang.error: unresolved compilation problem: method max(collection<? extends t>) in type collections not applicable arguments (int) @ array_6.main(array_6.java:23) my code follows:
int input; int highestnumber = 0; int arraynumber = 0; int[] intarray = new int[10]; system.out.println("this application determines highest number out of 10 inputed numbers"); system.out.println("welcome, please input first number:"); (int = 0; < 10; i++){ system.out.print(i+1 + ": "); input = textio.getlnint(); // retrieves input intarray[i] = input; } highestnumber = collections.max(arrayint); system.out.println("highest number " + highestnumber); edit
i did recommended below, , message
exception in thread "main" java.lang.error: unresolved compilation problems: type mismatch: cannot convert int[] int bound mismatch: generic method max(collection<? extends t>) of type collections not applicable arguments (list<int[]>). inferred type int[] not valid substitute bounded parameter <t extends object & comparable<? super t>> @ array_6.main(array_6.java:24) appears, changed int highestnumber array. error appears:
exception in thread "main" java.lang.error: unresolved compilation problem: bound mismatch: generic method max(collection<? extends t>) of type collections not applicable arguments (list<int[]>). inferred type int[] not valid substitute bounded parameter <t extends object & comparable<? super t>> @ array_6.main(array_6.java:24)
collections utility class collections api. arrayint array of int incompatible collections class offers
you try using...
highestnumber = collections.max(arrays.aslist(intarray)); which wraps intarray in list interface, making compatible collections api
as @louiswasserman has pointed out, above won't work, collections.max expecte object not primitive based collection.
the simplest solution might check each value it's entered, example....
for (int = 0; < 10; i++) { system.out.print(i + 1 + ": "); input = textio.getlnint(); // retrieves input intarray[i] = input; highestnumber = math.max(input, highestnumber); } or use arrays.sort(intarray) sort intarray , pick off last element in array, highest...
or create second loop , iterate through each element in intarray using math.max find largest value...
or create list<integer> collection , add each item intarray , use collections.max...
or use integer[] array instead of int[] array...
or create list<integer> collection instead of using arrays altogether...
Comments
Post a Comment