valeu...eu tb tou a tentar fazer com o HashSet...mas ele ta deixando inserir objectos iguais...
aqui esta a classe vertice
package dataStrutures;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
public class Main {
/**
* @param args
*/
public static class Vertice implements Comparable {
private String cidade;
private int hora;
public Vertice(String cidade, int hora) {
this.cidade = cidade;
this.hora = hora;
}
public Vertice() {
}
public int gethora() {
return hora;
}
public void sethora(int v_1) {
this.hora = v_1;
}
public String getcidade() {
return this.cidade;
}
public void setCidade(String city) {
this.cidade = city;
}
public String toString() {
return " " + this.cidade + " " + this.hora;
}
@Override
public int compareTo(Object o) {
int value = 0;
if (this.hora < ((Vertice) o).hora) {
value = -1;
} else if (this.hora > ((Vertice) o).hora) {
value = 1;
} else if (this.hora == ((Vertice) o).hora) {
value = 0;
}
return value;
}
public boolean equals(Vertice xpto) {
if (this.cidade.equals(xpto.getcidade())
&& this.hora == xpto.gethora()) {
return true;
} else
return false;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
ArrayList<Arcos> arcos = new ArrayList<Arcos>();
String[] data = new String[5];
int n_cidades = in.nextInt();
String cidade_partida = in.next();
String cidade_chegada = in.next();
int hora_chegada = in.nextInt();
int numero_voos = in.nextInt();
in.nextLine();
HashSet<Vertice> vertices = new HashSet<Vertice>();
Vertice v_inicial = new Vertice(cidade_partida, 0);
Vertice v_final = new Vertice(cidade_chegada, hora_chegada + 30);
changeHour(v_final);
vertices.add(v_inicial);
vertices.add(v_final);
for (int x = 0; x < numero_voos; x++) {
data = in.nextLine().split(" ");
Vertice v_partida = new Vertice(data[0], Integer.parseInt(data[3]));
Vertice v_chegada = new Vertice(data[1],
Integer.parseInt(data[4]) + 30);
changeHour(v_chegada);
if (!vertices.contains(v_partida)) {
vertices.add(v_partida);
}
if (v_chegada.gethora() <= hora_chegada
&& !vertices.contains(v_chegada)) {
vertices.add(v_chegada);
}
}
System.out.println(vertices.toString());
}
public static void changeHour(Vertice aux) {
if (String.valueOf(aux.gethora()).substring(2, 4).equals("60")) {
// aux.setHour((x+1));
aux.sethora(Integer.parseInt((String
.valueOf((Integer.parseInt(String.valueOf(aux.gethora())
.substring(0, 2)) + 1)) + "00")));
}
}
no final eu quero apenas 10 vertices neste hashSet, mas ele ta aceitar valores ou objectos repetidos!!! brincandeira....
Output:
[ berlin 1530, lisbon 1000, paris 1130, london 1130, london 1130, lisbon 0, london 1130, paris 1500, berlin 1300, paris 1130, lisbon 1300, paris 1330, london 1500, lisbon 1300, paris 1330, london 1340, berlin 1300, lisbon 1000]
