Lista em Ordem Decrescente

6 respostas
T

Bom Dia.

Tenho o seguinte código abaixo:

package vpda.bean;

import java.text.*;
import java.util.*;
import javax.swing.*;

public class s {
	public static void main(String args[]) {
		Collator collator = Collator.getInstance();
		int vetor[] = new int[10];
		Set set = new TreeSet();

		vetor = new int[10];

		for (int i = 0; i < vetor.length; i++) {
			CollationKey key1 =	collator.getCollationKey(JOptionPane.showInputDialog("Digite a Palavra: "));
			set.add(key1);
		}
		printCollectionCrescente(set);
		printCollectionDecrescente(set);
	}

	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("
 * * * * * * * * * * * * * * * ");
	}
}

Mas eu não sei decrementar.
Alguém tem alguma sugestão de como eu poderia fazer o método:

printCollectionDecrescente???

É uma lista ordenada, aonde vc insere 10 valores e ele ordena decrescentemente…
Mas eu tb quero em ordem decrescente.

Como eu faria?

Grata,

Tify

6 Respostas

T

É 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);
T
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?

T

Aham, fiz um erro (quem manda eu não compilar o programa antes de mandar?)

Em vez de

Collections.copy (decrescente, set);

faça:

decrescente.addAll(set);
T

Thiago,

O erro persiste.
É no método compareTo.

T

Outro erro - não é public int compareTo(Object,Object). é:

public int compare(Object o1, Object o2)

Sempre confundo “Comparator” com “Comparable”.

T

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

Criado 13 de outubro de 2004
Ultima resposta 13 de out. de 2004
Respostas 6
Participantes 2