How to compare, remove equals, etc elements of two vectors in java?

Asked

Viewed 1,567 times

1

I am doing a work in java to perform operations together, but as I am learning now I do not know how to compare the elements of vectors to make the following operations, union, intersection, difference and complement, I want to do the operations after printing the second set, someone could help me how I can do this in this code. Follow the code of how it is:

public static void main(String[] args) {
    Scanner scan= new Scanner(System.in);
    System.out.println("Insira seis números para o primeiro conjunto.");
    int[] var1 = new int[6];
        System.out.println("Digite o primeiro número: ");
        var1[0]= scan.nextInt();
        System.out.println("Digite o segundo número: ");
        var1[1]= scan.nextInt();
        System.out.println("Digite o terceiro número: ");
        var1[2]= scan.nextInt();
        System.out.println("Digite o quarto número: ");
        var1[3]= scan.nextInt();
        System.out.println("Digite o quinto número: ");
        var1[4]= scan.nextInt();
        System.out.println("Digite o sexto número: ");
        var1[5]= scan.nextInt();
    System.out.println("Insira mais seis números para o segundo conjunto.");
    int[] var2 = new int[6];
        System.out.println("Digite o primeiro número: ");
        var2[0]= scan.nextInt();
        System.out.println("Digite o segundo número: ");
        var2[1]= scan.nextInt();
        System.out.println("Digite o terceiro número: ");
        var2[2]= scan.nextInt();
        System.out.println("Digite o quarto número: ");
        var2[3]= scan.nextInt();
        System.out.println("Digite o quinto número: ");
        var2[4]= scan.nextInt();
        System.out.println("Digite o sexto número: ");
        var2[5]= scan.nextInt();
    System.out.println("");
    System.out.println("O primeiro conjunto é: ");
    for(int i=0; i<var1.length; i++) {
        System.out.print(var1[i]+", ");
    }
    System.out.println("");
    System.out.println("O segundo conjunto é: ");
    for(int a=0; a<var2.length; a++) {
        System.out.print(var2[a]+", ");
    }        
}

}

  • Does it have to be with an array or can it be a list? With a list it’s much easier

  • It may be anyway kkk, is that I was learning with array, but can be with list, still not quite sure the difference between the two

1 answer

0

Hello, see if this helps you. It’s a simple code, the way you put it. I just made a loop to read the entries (fewer lines of code) and, after printing the two vectors, I did the basic operations (join, elements in common, add and subtract). There are better ways to do that, but from what I understand, you’re starting now, right? I think it’s nice to focus on your problem, understand the manipulation of data from a vector, and then move on to more complex implementations using Java resources.

Good luck. =)

// Sendo bem pratico e simples.
import java.util.Scanner;

class Arrays {
    public static void main(String[] args) {
        Scanner scan= new Scanner(System.in);
        System.out.println("Insira seis números para o primeiro conjunto.");
        int[] var1 = new int[6];
        for ( int i=0; i<6; i++ ) {
            System.out.println("Digite um número: ");
            var1[i]= scan.nextInt();
        }

        System.out.println("Insira mais seis números para o segundo conjunto.");
        int[] var2 = new int[6];
        for ( int i=0; i<6; i++ ) {
            System.out.println("Digite um número: ");
            var2[i]= scan.nextInt();
        }

        System.out.println("");
        System.out.println("O primeiro conjunto é: ");
        for(int i=0; i<var1.length; i++) {
            System.out.print(var1[i]+", ");
        }

        System.out.println("");

        System.out.println("O segundo conjunto é: ");
        for(int a=0; a<var2.length; a++) {
            System.out.print(var2[a]+", ");
        }

        System.out.println("\nUniao:");
        // Uniao dos dois
        int[] uniao = new int[12];
        for ( int i=0; i<6; i++ ) 
            uniao[i] = var1[i];

        for ( int i=6; i<12; i++ ) 
            uniao[i] = var2[i-6];

        for ( int i=0; i<12; i++ )
            System.out.print(uniao[i] + ", ");

        System.out.println("\nInterseccao:");
        // Interseccao dos dois
        int [] interseccao = new int[6];
        for ( int i=0; i<6; i++ ) {
            for ( int j=0; j<6; j++ ) {
                if ( var1[i] == var2[j] ) {
                    interseccao[i] = var1[i];
                }
            }
        }

        for ( int i=0; i<6; i++ )
            System.out.print(interseccao[i] + ", ");

        System.out.println("\nSoma:");
        // Soma dos dois ( x(a,b) + y(c,d) = z(a+c,b+d) )
        int [] soma = new int[6];
        for ( int i=0; i<6; i++ ) {
            for ( int j=0; j<6; j++ ) {
                    soma[i] = var1[i] + var2[i];
            }
        }

        for ( int i=0; i<6; i++ )
            System.out.print(soma[i] + ", ");

        System.out.println("\nSubtracao:");
        // Diferenca dos dois ( x(a,b) - y(c,d) = z(a-c,b-d) )
        int [] subtracao = new int[6];
        for ( int i=0; i<6; i++ ) {
            for ( int j=0; j<6; j++ ) {
                    subtracao[i] = var1[i] - var2[i];
            }
        }

        for ( int i=0; i<6; i++ )
            System.out.print(subtracao[i] + ", ");

        System.out.println("\n");
    }
}
  • Thank you very much, now I’ll study and put my hand in the dough

  • You’re welcome, Elber. Anything, ask more questions or add to this. ;-)

  • Their union created a bag, not necessarily a set. Normally, the cardinality of the union is expected to be |A| + |B| - |A^B|, but its resulting set has cardinality |A| + |B|. You can see by passing the sets {1, 2, 3, 4, 5, 6} and {2, 3, 4, 5, 6, 7}, set expected result is {1,2,3,4,5,6,7}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.