andrielc 10 de abr. de 2013
Existe varias maneiras, uma continuação do que você estava fazendo:
public static void main ( String [] args ) {
List listA = new LinkedList ();
listA . add ( 100 );
listA . add ( 20 );
listA . add ( 300 );
listA . add ( 30 );
listA . add ( 80 );
listA . add ( 200 );
listA . add ( 40 );
listA . add ( 100 );
listA . add ( 200 );
iterator pos = listA . iterator ();
Integer soma = 0 ;
Integer qtd = 0 ;
while ( pos . hasnext ()){
soma += ( Integer ) pos . next ();
qtd ++ ;
}
System . out . println ( "Media = " + soma / qtd );
}
kleberdamasco 10 de abr. de 2013
Apenas para fazer ajustes no codigo...
andrielc:
Existe varias maneiras, uma continuação do que você estava fazendo:
public static void main ( String [] args ) {
List listA = new LinkedList ();
listA . add ( 100 );
listA . add ( 20 );
listA . add ( 300 );
listA . add ( 30 );
listA . add ( 80 );
listA . add ( 200 );
listA . add ( 40 );
listA . add ( 100 );
listA . add ( 200 );
Iterator < Integer > pos = listA . iterator ();
Integer soma = 0 ;
Integer qtd = 0 ;
while ( pos . hasnext ()){
soma += pos . next ();
qtd ++ ;
}
System . out . println ( "Media = " + soma / qtd );
}
GuilhermeENGMAT 10 de abr. de 2013
Valeu cara!!! ajudou demais!
regis_hideki 11 de abr. de 2013
1 - Use generics.
2 - Use double ou Double para calcular a média, e não int ou Integer.
3 - Por que dividiu por 8 se a lista tem 9 elementos? Um cara fez um contador e funciona, mas você pode usar o método “.size()”