thingol 13 de out. de 2004
É pena que não existe um “reverse_iterator” como em C++ (que pena). Em vez disso a maneira mais boba é definir um ArrayList, copiar o Set dentro dele, e reordená-lo em ordem decrescente.
Set set = new TreeSet ();
List decrescente = new ArrayList ();
for ( int i = 0 ; i < vetor . length ; i ++) {
CollationKey key1 = collator.getCollationKey(JOptionPane.showInputDialog("Digite a Palavra : " ));
set.add(key1) ;
}
Collections . copy ( decrescente , set );
Collections . sort ( decrescente , new Comparator () {
public int compareTo (Object o1, Object o2) {
CollationKey ck1 = (CollationKey) o1 ;
CollationKey ck2 = (CollationKey) o2 ;
return - ck1.compareTo (ck2) ;
}
} );
printCollectionDecrescente ( decrescente );
tifyzinha 13 de out. de 2004
import java.text.* ;
import java.util.* ;
import javax.swing.JOptionPane ;
public class lista {
public static void main ( String args [] ) {
Collator collator = Collator . getInstance ();
int vetor [] = new int [ 10 ] ;
Set set = new TreeSet ();
List decrescente = new ArrayList ();
vetor = new int [ 10 ] ;
for ( int i = 0 ; i < vetor . length ; i ++ ) {
CollationKey key1 = collator . getCollationKey ( JOptionPane . showInputDialog ( "Digite a Palavra: " ));
set . add ( key1 );
}
Collections . copy ( decrescente , ( List ) set );
Collections . sort ( decrescente , new Comparator () {
public int compareTo ( Object o1 , Object o2 ) {
CollationKey ck1 = ( CollationKey ) o1 ;
CollationKey ck2 = ( CollationKey ) o2 ;
return - ck1 . compareTo ( ck2 );
}
});
printCollectionCrescente ( set );
printCollectionDecrescente ( decrescente );
}
static private void printCollectionCrescente ( Collection collection ) {
boolean first = true ;
Iterator iterator = collection . iterator ();
System . out . println ( "* * * Lista Crescente * * * " );
while ( iterator . hasNext ()) {
if ( first ) {
first = false ;
} else {
System . out . print ( "
" );
}
CollationKey key = ( CollationKey ) iterator . next ();
System . out . print ( "
" + key . getSourceString ());
}
System . out . println ( "
* * * * * * * * * * * * * * * " );
}
static private void printCollectionDecrescente ( Collection collection ) {
boolean last = true ;
Iterator iterator = collection . iterator ();
System . out . println ( "* * * Lista Decrescente * * * " );
while ( iterator . hasNext ()) {
if ( last ) {
last = false ;
} else {
System . out . print ( "
" );
}
CollationKey key = ( CollationKey ) iterator . next ();
System . out . print ( "
" + key . getSourceString ());
}
System . out . println ( "
* * * * * * * * * * * * * * * " );
System . exit ( 0 );
}
}
Thiago, eu adicionei um cast ao List mas ainda esta dando um errinho.
Dá uma olhada no erro:
lista . java : 19 : copy ( java . util . List , java . util . List ) in java . util . Collections
ot be applied to ( java . util . List , java . util . Set )
Collections . copy ( decrescente , set );
^
lista . java : 21 : < anonymous lista $ 1 > should be declared abstract ; it does not
ne compare ( java . lang . Object , java . lang . Object ) in
public int compareTo ( Object o1 , Object o2 ) {
^
Alguma sugestão?
thingol 13 de out. de 2004
Aham, fiz um erro (quem manda eu não compilar o programa antes de mandar?)
Em vez de
Collections . copy ( decrescente , set );
faça:
tifyzinha 13 de out. de 2004
Thiago,
O erro persiste.
É no método compareTo.
thingol 13 de out. de 2004
Outro erro - não é public int compareTo(Object,Object). é:
public int compare ( Object o1 , Object o2 )
Sempre confundo “Comparator” com “Comparable”.
tifyzinha 13 de out. de 2004
Thiago, muito obrigada.
Funcionou.
Para quem quiser saber como ficou o código:
import java.text.* ;
import java.util.* ;
import javax.swing.JOptionPane ;
public class lista {
public static void main ( String args [] ) {
Collator collator = Collator . getInstance ();
int vetor [] = new int [ 10 ] ;
Set set = new TreeSet ();
List decrescente = new ArrayList ();
vetor = new int [ 10 ] ;
for ( int i = 0 ; i < vetor . length ; i ++ ) {
CollationKey key1 = collator . getCollationKey ( JOptionPane . showInputDialog ( "Digite a Palavra: " ));
set . add ( key1 );
}
decrescente . addAll ( set );
Collections . sort ( decrescente , new Comparator () {
public int compare ( Object o1 , Object o2 ) {
CollationKey ck1 = ( CollationKey ) o1 ;
CollationKey ck2 = ( CollationKey ) o2 ;
return - ck1 . compareTo ( ck2 );
}
});
printCollectionCrescente ( set );
printCollectionDecrescente ( decrescente );
}
static private void printCollectionCrescente ( Collection collection ) {
boolean first = true ;
Iterator iterator = collection . iterator ();
System . out . println ( "* * * Lista Crescente * * * " );
while ( iterator . hasNext ()) {
if ( first ) {
first = false ;
} else {
System . out . print ( "
" );
}
CollationKey key = ( CollationKey ) iterator . next ();
System . out . print ( "
" + key . getSourceString ());
}
System . out . println ( "
* * * * * * * * * * * * * * * " );
}
static private void printCollectionDecrescente ( Collection collection ) {
boolean last = true ;
Iterator iterator = collection . iterator ();
System . out . println ( "* * * Lista Decrescente * * * " );
while ( iterator . hasNext ()) {
if ( last ) {
last = false ;
} else {
System . out . print ( "
" );
}
CollationKey key = ( CollationKey ) iterator . next ();
System . out . print ( "
" + key . getSourceString ());
}
System . out . println ( "
* * * * * * * * * * * * * * * " );
System . exit ( 0 );
}
}
Att