Privacidad: Recuerde que la información escrita en los foros de programación es 100% pública y que su ip será registrada asociada a su mensaje. Si encuentra un mensaje fuera de lugar, por favor, notifiquelo para su revisión y eliminación.
ordenar collection
Enviado por guiru25 el día 8 de marzo de 2008
hola. estoy haciendo un programa para ordenar una lista de numeros que doy. he conseguido que funcione todo pero a la hora de ordenar la lista, no se ya como hacerlo. tengo que hacerlo mediante un Collection, pero no hay manera. haber si alguien sabe que es lo que me falla. no creo que sea mucho ya...
muchas gracias.
un saludo
/**
* Stores an array of integers and allows adding and sorting it
* @author Object-oriented programming teaching staff
* @version 2007-09-27
*/
import java.util.*;
public class NumberCollection {
/** array storage for integers */
private static int elements[];
/** number of elements currently in the array */
private static int quantity;
/**
* Class constructor: must be invoked before any operation on the class instance
* @param capacity maximum capacity of the array collection
*/
public void Collection(int capacity) {
elements= new int[capacity];
quantity= 0;
}
/** @return The number of elements currently stored in the array */
public int size() {
return quantity;
}
/**
* Adds an integer to the array, if it is not full
* @param elem The element to be added to the array
* @return false if the array is full, else true
*/
public boolean add(int elem) {
if (quantity== elements.length) {
System.err.println(\\\\\\\"Error: full collection, cannot add\\\\\\\");
return false;
} else {
elements[quantity]+= elem;
return true;
}
}
/**
* Sorts the array by ascending numbers
*/
public static boolean sort() {
boolean swapped= false;
boolean fi = false;
int upperLimit= quantity - 1;
while ((upperLimit > 0) && (!fi)) {
for (int j= 0; j < upperLimit; j++) {
if (!isSorted(j, j + 1))
swap(j, j + 1);
swapped= true;
}
if (!swapped) {
fi= true;
}
}
return fi;
}
/**
* Swaps contents of two positions (pos1 and pos2) in the array
*/
private static void swap(int pos1, int pos2) {
int tmp= elements[pos1];
elements[pos1]= tmp;
elements[pos2]= elements[pos2];
}
/**
* Checks if elements of two positions (pos1 and pos2) are sorted in ascending order
* @returns true if pos1 <pos2>= elements[pos2]);
}
/**
* Builds and returns an String representation of the array contents
* @return A blank-separated string of integers
*/
public String toString() {
StringBuffer s= new StringBuffer();
for (int i= 0; i < quantity; i++)
s.append(elements[i]).append(\\\\\\\" \\\\\\\");
return s.toString();
}
/**
* Main program, which tests the NumberCollection for several operations
* @param args Command-Line Arguments
*/
public static void main(String[] args) {
// Testing a number collection...
// First an instance is created:
Collection<Integer> c = new ArrayList<Integer>(10); ;
// Then numbers are inserted :
System.out.println(\\\\\\\"Inserting numbers...\\\\\\\");
c.add(45);
c.add(23);
c.add(9);
c.add(37);
c.add(46);
System.out.println(c.size() + \\\\\\\" inserted numbers\\\\\\\");
System.out.println(c.toString());
El método sort() del inteface Collections, necesita como argumento un objeto List, por lo que tendrás que convertir tu Collections en un List para ordenarlo y luego copiar el List a tu Colecctions...
System.out.println(c.toString());
List lista = Collections.list(Collections.enumeration(c));
Collections.sort(lista);
c = lista;
System.out.println(c.toString());