Ajuda em hashMap

3 respostas
Alkamavo

duvida Basica…

pretendo construir um hashMap<Integer, vertice> xpto…onde o vertice é um Objecto composto por uma string k é o nome de uma cidade e um inteiro que é a hora da cidade.

a minha dificuldade ta em nao introduzir uma cidade e a sua hora repetida no hashMap…

Input é:

lisbon london 6 1000 1100 london lisbon 6 1130 1230 lisbon paris 5 1000 1100 paris lisbon 4 1130 1230 london paris 1 1130 1300 london berlin 2 1340 1510 berlin london 2 1300 1430 paris berlin [telefone removido] berlin paris 9 1300 1430

neste caso

o vertice sera para a primeira linha

vertice 1= lisbon 1000;
vertice 2= london 1100;

podem ignorar o 6 k ta la no meio…

aguardo ajudas…

valeu parceiros…

o outputfinal sem repetiçoes:

(lisbon, 10:00)   
(lisbon, 13:00)
(london, 15:00) 
(london, 11:30)
(berlin, 13:00) 
(berlin, 15:30)
(paris, 11:30)
(paris, 15:00)

3 Respostas

danielbchaves

Não entendi a tua lógica… não achei esse “lisbon 13:00” que tem na segunda linha do resultado na tabela inicial que colocou…

talvez vc consiga algo usando um HashSet ou algo do tipo, vc poderia juntar o nome da cidade com a hora e adicionar no Set, ele não vai permitir Objetos iguais, no final vc teria todas as cidades/hora diferentes, porém não teria na mesma ordem que adicionou

Alkamavo

a ordem n importa...
olhe eu estou a tentar assim...

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		ArrayList<Arcos> arcos = new ArrayList<Arcos>();
		ArrayList<Vertice> partida = new ArrayList<Vertice>();
		ArrayList<Vertice> chegada = new ArrayList<Vertice>();
		HashMap<Integer, Vertice> build = new HashMap<Integer, Vertice>();
		ArrayList<Vertice> final_vertices = new ArrayList<Vertice>();
		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();

		HashMap<Integer, Vertice> all = new HashMap<Integer, Vertice>();

		Vertice v_inicial = new Vertice(cidade_partida, 0000);
		Vertice v_final = new Vertice(cidade_chegada, hora_chegada + 30);
		changeHour(v_final);
		chegada.add(v_final);
		partida.add(v_inicial);
		final_vertices.add(v_inicial);
		final_vertices.add(v_final);

		for (int x = 0; x < numero_voos; x++) {

			data = in.nextLine().split("\s");
			Vertice v1 = new Vertice(data[0], Integer.parseInt(data[3]));
			Vertice v2 = new Vertice(data[1], Integer.parseInt(data[4]) + 30);
			changeHour(v2);

			if (!(partida.contains(v1))) {
				partida.add(v1);
			}
			if (!(chegada.contains(v2))) {
				chegada.add(v2);
			}

		}
		
		for (int z = 0; z < numero_voos; z++) {
			if (!(final_vertices.contains(partida.get(z)))) 
				final_vertices.add(partida.get(z));
			
		}
		for (int j = 0; j < numero_voos; j++) {
			if (!(final_vertices.contains(chegada.get(j))))
				final_vertices.add(chegada.get(j));

		}


			for (int i = 0; i < final_vertices.size(); i++) {
				System.out.println(final_vertices
						.indexOf(final_vertices.get(i))
						+ " " + final_vertices.get(i)  + "  ");
			}
		}
depois no fim so vou colocar com com um ciclo for
all.put(arralist.indexof(get(i)), arralyst.getvertice));

mas no fim da-me mais do k kero..
0 lisbon 0 1 berlin 1530 2 lisbon 1000 3 london 1130 4 lisbon 1000 5 paris 1130 6 london 1130 7 london 1340 8 berlin 1300 9 paris 1330 10 london 1130 11 lisbon 1300 12 paris 1130 13 lisbon 1300 14 paris 1330 15 berlin 1540 16 london 1500 17 berlin 1530 [code]
danielbchaves

para usar o contains a objeto que está no ArrayList precisa ter o método equals implementado, pois é ele que será usado para verificar se o objeto está contido na coleção…

no resto acho que está indo por um caminho bom, vou fazer um teste aqui com o Set, de funcionar como estou pensando posto aqui pra vc

Criado 28 de junho de 2008
Ultima resposta 28 de jun. de 2008
Respostas 3
Participantes 2