Array java shows specific items

1

Hello, I have a question, for example, I have Array A {1,2,3,4,5}, I make another array B {1,2,3} with the items I want to delete from array A, and show the remaining items ex 4 and 5, however I'm stopping an if inside a for and the answer comes out completely crazy, could algume help me?

I can not use arraylist.

ex: A = {1,2,3,4,5}

new array with items that I wish not to be displayed:

B = {1,2,3}

inside the for stop and show only the different items

4 and 5

Below is the code I tried

int qtdpessoas ;
        int exclui ;
        Scanner sc = new Scanner(System.in);
        qtdpessoas = sc.nextInt();
        int idpessoa [] = new int [qtdpessoas];


        for (int i = 0; i < idpessoa.length; i++) {

            idpessoa[i]= sc.nextInt();
            }

        exclui = sc.nextInt();
        int paraexcluir [] = new int [exclui];

        for (int i = 0; i < paraexcluir.length; i++) {
            paraexcluir[i]= sc.nextInt();
            }

        for (int i = 0; i < idpessoa.length; i++) {
            for (int j = 0; j < paraexcluir.length; j++) {
                if (idpessoa[i]!= paraexcluir[j]) {
                System.out.println(idpessoa[i]);
                }
            }
    
asked by anonymous 19.02.2018 / 03:16

1 answer

1

To get the result you want to make the following changes in your code:

  • In%% check that the elements are equal, and if so, skip to the next iteration of the loop;
  • Add a if to check if it is in the last element and if you are using else if , you will have tested all elements and this condition will only be satisfied if no element is equal.

Your System.out.println will look something like this:

if (idpessoa[i] == paraexcluir[j]) {
  break;
} else if (j == (paraexcluir.length - 1)) {
  System.out.println(idpessoa[i]);
}
    
19.02.2018 / 05:12