Duvida fazendo uma funcao List

2 respostas
dicabeca
Pessoal tenho duas Classes a meu problema comeca ai na classe grafo tenho uma funcao lista que passa minha List lx e a funcao SizeLista q passa o tamanho dela,blz no local printa tudo certinho tamanho 6 e e minha Lista A,B,C,D,E,F, mas na minha outra classe Vertice qnd eu pego o tamanho da List vem 12 nao to entendendo pq alguem pode me ajudar;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; 
import java.util.HashMap;
import java.util.Map;

public class grafo implements Serializable {
	
	private static final long serialVersionUID = 1L;
	public static Map<String,Double> p = new HashMap<String,Double>();
	public static List<String> lx = new ArrayList<String>();
	public static List<String> ly = new ArrayList<String>();
	public static List<String> tab = new ArrayList<String>();
	public int k;
	public boolean liga;
	public String combo[];
	public grafo()
	 {
	 	 p.put("A",235.000);
		 p.put("B",321.000);
		 p.put("C",87.000);
		 p.put("D",102.000);
		 p.put("E",46.500);
		 p.put("F",647.000);
		 
		 lx.add("A");
		 lx.add("B");
		 lx.add("C");
		 lx.add("D");
		 lx.add("E");
		 lx.add("F");
		 
		 ly.add("A");
		 ly.add("B");
		 ly.add("C");
		 ly.add("D");
		 ly.add("E");
		 ly.add("F");
	
   	  tab.add(ly.get(0) +","+ lx.get(2));   	 
   	  tab.add(ly.get(1) +","+ lx.get(2));   	 
   	  tab.add(ly.get(1) +","+ lx.get(5));   	 
   	  tab.add(ly.get(2) +","+ lx.get(0));   	 
   	  tab.add(ly.get(2) +","+ lx.get(1));   	 
   	  tab.add(ly.get(2) +","+ lx.get(3));   	 
   	  tab.add(ly.get(3) +","+ lx.get(2));   	 
   	  tab.add(ly.get(3) +","+ lx.get(4));   	 
   	  tab.add(ly.get(4) +","+ lx.get(3));   	 
   	  tab.add(ly.get(4) +","+ lx.get(5));   	 
   	  tab.add(ly.get(5) +","+ lx.get(1));
   	  tab.add(ly.get(5) +","+ lx.get(4));
   	 
	 }
	
	public int sizeLista()
	{
		return lx.size();
	} 	 
	public List<String> lista()
	{
		return lx;
	}	

   public boolean Ligacoes(String y,String x)
   { 
   	 String z = y +","+ x;
   	 k = 0;
   	 for(int i=0;i < tab.size();i++)
   	  {
   	  	if(z.equals(tab.get(i)))
   	  	k = 1;
   	  }
   	 if(k > 0)
   	  return true;
   	 else
   	  return false;	 
   }
   
   public void AdicionaVertice(String x,Double y)
   {
	 p.put(x,y);
   }
   public void RemovoVertice(String x)
   {
   	 p.remove(x);
   }   
 
 public class visualizacaoGrafo extends JPanel
   {
   	
   	private static final long serialVersionUID = 6994470617748035985L;
   	
    
	 	protected void paintComponent(Graphics g)
	 	 {
	 	 	// Não se pode mudar o estado do objeto g, então fazemos uma cópia dele.
	 		Graphics2D g2d = (Graphics2D) g.create();
	 		
	 		g2d.setColor(Color.black);
	 		g2d.drawArc(15,35,80,80,0,360);
	 	 }
 	 
   }
 
   public static void main(String args[])
   {
   	 grafo g = new grafo();
   	 System.out.println(g.sizeLista());//g.Ligacoes("A","C"));
   }
  
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; 
 
 public class Vertice extends JFrame implements Serializable
 	{
 		private static final long serialVersionUID = 1L;
	  	private JTextField TXT_Ponto,TXT_Hab;
	  	private JLabel l1,l2,l3;
	  	private JButton b1,b2,b3;
	  	private JComboBox cb1;
	  	public grafo g;
	  	public char funcao;
	  	public int x,z;
	  	public static List<String> combo = new ArrayList<String>();
	  	
	  	public Vertice(char f)
	  	{	
	  		super("Vertices");
	  		g = new grafo();
	  		setLayout(new BorderLayout());
			JPanel panelAddVert = new JPanel();
			funcao = f;
			
			
			panelAddVert.setLayout(null);
			
			l1 		  = new JLabel();
			l2 		  = new JLabel();
			l3 		  = new JLabel();
			TXT_Ponto = new JTextField();
			TXT_Hab	  = new JTextField();
			b1 		  = new JButton();
			b2 		  = new JButton();
			cb1  	  = new JComboBox();
			
			//b3		  = new JButton();
			switch (f)
			 {
				case 'A': 				 
				 l1.setText("Cidade:");
				 l2.setText("Habitantes:");
				 l3.setText("Associe a um Ponto:");
				 b1.setText("Inserir");
				 b2.setText("Cancelar");
				 panelAddVert.add(l1);
				 panelAddVert.add(TXT_Ponto);
				 panelAddVert.add(l2);
				 panelAddVert.add(TXT_Hab);
				 panelAddVert.add(l3);
				 cb1.setEditable(true);
				 for(int i=0;i < combo.size();i++)
				 {
				 	cb1.addItem(combo.get(i));
				 }
				 panelAddVert.add(cb1);
				 panelAddVert.add(b1);
				 panelAddVert.add(b2);
				 x = 60;
				 z = 50;
				break;
				case 'R':
				 l1.setText("Qual Ponto:");
				 //l2.setText("Associe a um Ponto.");
				 b1.setText("Excluir");
				 b2.setText("Cancelar");				
				 panelAddVert.add(l1);
				 panelAddVert.add(TXT_Ponto);
				 panelAddVert.add(b1);
				 panelAddVert.add(b2);
				 x = 90;
				 z = 130;
				break;
			 }
			 //jtf.setDocument(new LimitDocument(1)); //somente 1 caracter
			 
			add(panelAddVert);
			
			l1.setBounds(5,35,z,20);
			TXT_Ponto.setBounds(x,35,50,20);
			l2.setBounds(150,35,100,20);
			TXT_Hab.setBounds(220,35,50,20);
			l3.setBounds(5,65,130,20);
			cb1.setBounds(150,65,100,20);
			b1.setBounds(50,100,100,20);
			b2.setBounds(170,100,100,20);
			
			//b3.setBounds(150,120,100,20);
			
			inseriVertice acao = new inseriVertice();
			b1.addActionListener(acao);
			b2.addActionListener(acao);
			cb1.addActionListener(acao);
			//b3.addActionListener(acao);
	    }
	    
	    private class inseriVertice implements ActionListener
	    {	
	    	
	    	public void actionPerformed(ActionEvent evento)
	    	{
	    		if(evento.getSource() == b1)
	    		   if(funcao == 'A')
	    			{
	    			 g.AdicionaVertice(TXT_Ponto.getText(),Double.parseDouble(TXT_Hab.getText()));
	    			 Vertice.this.dispose();
	    			} 
	    		   if(funcao == 'R')
	    			{
	    			 g.RemovoVertice(TXT_Ponto.getText());
	    			 Vertice.this.dispose();
	    			}
	    		if(evento.getSource() == b2)
	    			//Vertice.this.dispose();
	    			System.out.println(g.sizeLista());
	    	}
	    }
}

2 Respostas

Giulliano

Caramba tem bastante código aí hein…rsss

escuta faz um teste…Cria uma nova List e armazena a sua lista nela (como se vc estivesse buscando dados num bd)

Depois dá um size na sua lista nova e veja o tamanho …se continuar 12…

Imprima sua lista nova e veja o por que de 12 elementos…

[/]'s

dicabeca

ja fiz isso e me retorna 12 do mesmo jeito ta duplicando os dados so nao sei o pq de disso.

Criado 29 de maio de 2007
Ultima resposta 29 de mai. de 2007
Respostas 2
Participantes 2