Procurando uma String dentro d um List/vetor

2 respostas
dicabeca
pessoal tem como eu procurar uma String dentro d uma List/vetor,meu caso e o seguinte tenho uma List tab, q qnd eu adiciono eu escolho um ponto concateno com uma "," e concateno com um outro ponto,com isso posso ter varios pontos relacionados,Map nao serve para o meu caso,por exemplo posso ter as seguinte relações: "C,A" "C,E" "B,A" "D,E" e etc.... ,o q eu quero e alguma funcao q eu procure na minha List q comece com C,me retornando a "C,A" e a"C,E" tem como fzr isso?
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;
import java.lang.Object;

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[];
	
	 static 
     {
         grafo.lx.add("A");
		 grafo.lx.add("B");
		 grafo.lx.add("C");
		 grafo.lx.add("D");
		 grafo.lx.add("E");
		 grafo.lx.add("F");
		 
		 grafo.ly.add("A");
		 grafo.ly.add("B");
		 grafo.ly.add("C");
		 grafo.ly.add("D");
		 grafo.ly.add("E");
		 grafo.ly.add("F");
	
   	  grafo.tab.add(ly.get(0) +","+ lx.get(2));
   	  grafo.tab.add(ly.get(1) +","+ lx.get(2));
   	  grafo.tab.add(ly.get(1) +","+ lx.get(5));
   	  grafo.tab.add(ly.get(2) +","+ lx.get(0));
   	  grafo.tab.add(ly.get(2) +","+ lx.get(1));
   	  grafo.tab.add(ly.get(2) +","+ lx.get(3));
   	  grafo.tab.add(ly.get(3) +","+ lx.get(2));
   	  grafo.tab.add(ly.get(3) +","+ lx.get(4));
   	  grafo.tab.add(ly.get(4) +","+ lx.get(3));
   	  grafo.tab.add(ly.get(4) +","+ lx.get(5));
   	  grafo.tab.add(ly.get(5) +","+ lx.get(1));
   	  grafo.tab.add(ly.get(5) +","+ lx.get(4));
     }
	
	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);   	 
	 }
   
   public void AdicionaVertice(String x,Double y,String z)
    {
	  p.put(x,y);
	  lx.add(x);
	  ly.add(x);
	  tab.add(x+","+z);	 
    }
   public void RemovoVertice(String x)
    {
   	 	p.remove(x);
   	 	lx.remove(x);
   	 	ly.remove(x);
    }  
	public List MostraGrafo()
	{
	  	return tab;
	}	
		 
	public List 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 List v2()
   {
   	 return tab;
   }
  
   public static void main(String args[])
   {
   	 grafo g = new grafo();
   	 System.out.println(g.v2());
   }
  
}

2 Respostas

fabiozoroastro

Oi dicabeca, eu não li o seu tópico todo, mas pra vc verificar se existe uma string numa lista de string utilize:

lista.contains(suaString);

Aproveita e dá uma olhada:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html#contains(java.lang.Object)

R

seria algo como:

for (String a : listaString) {
  char c = a.charAt(0);
   if (c == 'C'){
	// faça o que vc quiser
   }
}

deve ter uma forma mais simples e melhor… mas isto deve resolver…

Criado 30 de maio de 2007
Ultima resposta 30 de mai. de 2007
Respostas 2
Participantes 3