Friday, 15 January 2010

java - LinkedList : Collections.max() throwing NoSuchElementException -



java - LinkedList : Collections.max() throwing NoSuchElementException -

i not iterating linkedlist means scanner or other methods, using collections.max() maximum number linkedlist.

i have read on stack overflow exception thrown due iterator or scanner or tokenizer, using none of them.

import java.io.*; import java.util.*; class tlg { public static void main(string[] args)throws ioexception { bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); linkedlist<integer> first = new linkedlist<integer>(); linkedlist<integer> sec = new linkedlist<integer>(); int cases = integer.parseint(br.readline()); for(int i=1;i<=cases;i++) { string score = br.readline(); int number1 = integer.parseint(score.split(" ")[0]); int number2 = integer.parseint(score.split(" ")[1]); int diff = number1 - number2; if(diff > 0){ first.add(diff); } else { second.add(java.lang.math.abs(diff)); } } integer max1 = collections.max(first); // getting exception here integer max2 = collections.max(second); // getting exception here if(max1 > max2) { system.out.println(1+" "+max1); } else { system.out.println(2+" "+max2); } } }

/** * returns maximum element of given collection, according * <i>natural ordering</i> of elements. elements in * collection must implement <tt>comparable</tt> interface. * furthermore, elements in collection must <i>mutually * comparable</i> (that is, <tt>e1.compareto(e2)</tt> must not throw * <tt>classcastexception</tt> elements <tt>e1</tt> , * <tt>e2</tt> in collection).<p> * * method iterates on entire collection, hence requires * time proportional size of collection. * * @param coll collection maximum element determined. * @return maximum element of given collection, according * <i>natural ordering</i> of elements. * @throws classcastexception if collection contains elements * not <i>mutually comparable</i> (for example, strings , * integers). * @throws nosuchelementexception if collection empty. <--------------- * @see comparable */ public static <t extends object & comparable<? super t>> t max(collection<? extends t> coll)

java collections nosuchelementexception

No comments:

Post a Comment