Comparar vetores

5 respostas
J

Quero comparar dois vetores e verificar se o conteúdo dos mesmos é igual e está na mesma sequência. Sei que posso fazer isso usando for.Mas existe outra maneira mais eficaz?

5 Respostas

pedromuyala

for(int i = 0; i < posicoes; i++){ if(vet1[i] != vet2[i]) return }

EDITADO:
== para inteiros!
.equals() para objetos!

:wink:

J

Já tinha feito isso. Mas eu achei que já tivesse algum método pronto em java.

Andre_Fonseca

pode fazer assim

String[] v1 = {"um","dois","tres"};
String[] v2 = {"Um","dois","tres"};
String[] v3 = {"um","dois","tres"};

if (Arrays.equals(v1, v2)) {
  System.out.println("v1 igual v2");
}
		
if (Arrays.equals(v1, v3)) {
  System.out.println("v1 igual v3");
}
MarcioCasteloBranco
int [] vetor1 = new int[4];
        int [] vetor2 = new int[4];

        vetor1[0] = 1;
        vetor1[1] = 2;
        vetor1[2] = 3;
        vetor1[3] = 4;

        vetor2[0] = 4;
        vetor2[1] = 3;
        vetor2[2] = 2;
        vetor2[3] = 1;

        if (Arrays.equals(vetor1, vetor2)) {//retorna uma booleana
            System.out.println("lista iguais");
        } else {
            System.out.println("listas diferentes");
        }

        if (vetor1.hashCode() == vetor2.hashCode()) {// retorna dois inteiros que são comparados
            System.out.println("lista iguais");
        } else {
            System.out.println("listas diferentes");
        }

Espero ter ajudado!!!
vlw

peresjuliao

Olá?

Segue um exemplo clássico.:
import java.util.*;
import java.io.*;
 
public class ComparingIntegers
{
    public static int[] stringToArray(String s)
    {
   
        String u = "";
             
        String[] tokens = s.split(" ");
        int o = 0;    
                 
   
            for (int i = 0; i < tokens.length; i++) {
               try{ 
                   int a = Integer.parseInt(tokens[i]);
                
 
                    u = a + " ";
                    o++;
                
            }catch (NumberFormatException e){
        }
    }   
            
        
        int[] z = new int[o];
        int b = 0;
        String[] newTokens = u.split(" ");
        Scanner n = new Scanner(u);
        
 
        for(int i = 0; i<newTokens.length; i++){
                b = n.nextInt();
                z[i] = b;
        }
 
        return z;          
    }
        
 
    
    public static boolean containSameElements(int[] a, int[] b)
    {   
        int t=0;
        if(a.length == b.length){
               for(int i=0; i< b.length; i++){
                   
                    if(a[t] == b[i]){
                        for(int j = i; i < b.length-1; j++){
                            b[j] = b[j+1];
 
                        }
                        
                        
                    }else{
                        System.out.println("These two sets of data do not contain the same integers.");
                        return false;
                }
            }
            
            System.out.println("Both arrays contain the same integers.");
            return true;
            
            
            
        }else{
            System.out.println("These two sets of data to not contain the same integers.");
            return false;
        }
        
    }
 
    public static void main(String[] args)
    {
        Scanner v = new Scanner(System.in);
        System.out.println("Please enter your first set of values: ");
        String c = v.nextLine();
        System.out.println("Please enter your second set of values: ");
        String d = v.nextLine();
        
 
        int [] a = new int [stringToArray(c).length];
        a=stringToArray(c);
 
       
        int [] b = new int [stringToArray(d).length];
        b=stringToArray(d);
        
        containSameElements(a,b);
    }
}

Espero ter ajudado

Criado 20 de outubro de 2009
Ultima resposta 20 de out. de 2009
Respostas 5
Participantes 5