Ir ao conteúdo
  • Cadastre-se

Vanderleia da S. Montenegr

Membro Júnior
  • Posts

    1
  • Cadastrado em

  • Última visita

Reputação

0
  1. Boa noite, preciso implementar um programa em java que saiba quantos grafos e quais componentes conexas foram usadas para formar o grafo. lido de um arquivo TXt, fazer a leitura eu consegui so não estou conseguindo montar.... import java.util.List; import java.util.ArrayList; public class Grafo { public class Vertice { String nome; List<Aresta> adj; Vertice(String nome) { this.nome = nome; this.adj = new ArrayList<Aresta>(); } void addAdj(Aresta e) { adj.add(e); } } public class Aresta { Vertice origem; Vertice destino; Aresta(Vertice origem, Vertice destino) { this.origem = origem; this.destino = destino; } } List<Vertice> vertices; List<Aresta> arestas; public Grafo() { vertices = new ArrayList<Vertice>(); arestas = new ArrayList<Aresta>(); } Vertice addVertice(String int ) { Vertice v = new Vertice(int); vertices.add(v); return v; } Aresta addAresta(Vertice origem, Vertice destino) { Aresta e = new Aresta(origem, destino); origem.addAdj(e); arestas.add(e); return e; } } public class TestGrafo { /** * @param args the command line arguments * @throws java.io.IOException */ @SuppressWarnings("empty-statement") public static void main(String[] args) throws IOException { // TODO code application logic here File arq = new File ("C:\\Users\\wand\\Documents\\NetBeansProjects\\grafo\\src\\grafow\\Arquivo.txt"); try{ // verifica se o arquivo exiiste true boolean existe= arq.exists(); //false cria um arquivo vazio //boolean createNewFile = arq.createNewFile(); System.out.println("arquivo existe \n"+ existe); //criar arquivo arq.createNewFile(); FileReader fr = new FileReader(arq); BufferedReader br = new BufferedReader (fr); // enquanto tiver linha while (br.ready()){ //le proxima linha String linha = br.readLine(); List a = new ArrayList(); a.add(linha); for (int i=0; i<a.size(); i++){ for (int j=0; j< a.size(); j ++) System.out.println(a.get(i)); } } fr.close(); br.close(); }catch (IOException e){ } } } o meu arquivo lido arquivo existe true 1 2 3 4 5 6 7 8 9 10 1 3 1 8 2 6 3 7 3 8 2 6 3 7 3 8 4 5 4 10 CONSTRUÍDO COM SUCESSO (tempo total: 9 segundos) agora tenho que ler e aparecer por exemplo Quantidade: 4 grafos 1 3 7 8 2 6 4 5 10 9, não consigo terminar... se puderem me ajudar agradeço sou nova na area de programação

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...