I can not generate this second vector

1

Type a program that loads a vector with 10 integers and sorts it in ascending order using the bubble sort method.

The program should generate a second vector without the repeated numbers.

package com.bubble.sort;
import java.util.Scanner;

public class bubble_sort {
    private static Scanner teclado;

    public static void main(String args []) {
        int vet1[] = new int [10];
        int vet2[] = new int [10];
        int n, i, aux,troca;

        teclado = new Scanner(System.in);
        //Carregando os números do vetor:
        for(i=0;i<=9;i++) {

            System.out.print("Digite "+(i+1)+"º número do vetor:");
            vet1[i]= teclado.nextInt();
            vet2[i] = vet1[i];
        }
        n = 1;
        troca = 1;
        while (n <= 10 && troca == 1) {

            troca = 0;
            for(i=0;i<=8;i++) {

                if(vet1[i] > vet1[i+1]) {

                    troca = 1;
                    aux = vet1[i];
                    vet1[i] = vet1[i+1];
                    vet1[i+1] = aux;

                }

            }
            n = n + 1;
        }

        for(i=0;i<=9;i++) {
            System.out.print("["+vet1[i]+"]");
        }

    }
}
    
asked by anonymous 13.09.2017 / 18:26

1 answer

0

Remembering the remaining positions of the array that are not used, and should be ignored when displaying the array.

int valorAnterior =  vet1[0];
int contatodor = 0;
for(i=1;i<=9;i++) {
  if(valorAnterior != vet1[i]){
    vet2[contatodor] = vet1[i];
    valorAnterior = vet1[i];
    contatodor++;
  }
}

Complete code:

package com.bubble.sort;
import java.util.Scanner;

public class bubble_sort {
    private static Scanner teclado;

    public static void main(String args []) {
        int vet1[] = new int [10];
        int vet2[] = new int [10];
        int n, i, aux,troca;

        teclado = new Scanner(System.in);
        //Carregando os números do vetor:
        for(i=0;i<=9;i++) {

            System.out.print("Digite "+(i+1)+"º número do vetor:");
            vet1[i]= teclado.nextInt();
            vet2[i] = vet1[i];
        }
        n = 1;
        troca = 1;
        while (n <= 10 && troca == 1) {

            troca = 0;
            for(i=0;i<=8;i++) {

                if(vet1[i] > vet1[i+1]) {

                    troca = 1;
                    aux = vet1[i];
                    vet1[i] = vet1[i+1];
                    vet1[i+1] = aux;

                }

            }
            n = n + 1;
        }

        for(i=0;i<=9;i++) {
            System.out.print("["+vet1[i]+"]");
        }

        int valorAnterior =  vet1[0];
        vet2[0] =  vet1[0];
        int contatodor = 1;
        for(i=1;i<=9;i++) {
          if(valorAnterior != vet1[i]){
            vet2[contatodor] = vet1[i];
            valorAnterior = vet1[i];
            contatodor++;
          }
        }

        for(i=0;i<contatodor;i++) {
            System.out.print("["+vet2[i]+"]");
        }

    }
}
    
13.09.2017 / 18:41