Error when trying to print vectors (they change their values)

0

I have problems when printing some vectors.

I must fill 2 vectors of 5 positions each, then go through those vectors and if there are repeated values exchange those values repeated by -1, but keeping one of them and storing that modified vector in an auxiliary vector since the original vector does not you can touch.

Example:

vec1={1, 3, 5, 3, 6}   aux1={1, 3, 5, -1, 6}

vec2={1, 2, 2, 3, 5}   aux2={1, 2, -1, 3, 5}

The problem is that the code is printing me this Assuming that I fill the vector with these values: vec1={1, 3, 5, 3, 6}, vec2={1, 2, 2, 3, 5} print:

vec1={1, 3, 5, -1, 6}   aux1={1, 3, 5, -1, 6}

vec2={1, 2, -1, 3, 5} aux2={1, 2, -1, 3, 5}

but I do not know because since vector vec1 and vec2 never modified it

    public class revisar {
    static Scanner sc = new Scanner(System.in);
    static int d1=0, d2=0;
    static int vec1[]=new int[5]; 
    static int vec2[]=new int[5];
    static int aux1[]=new int[5];
    static int aux2[]=new int[5]; 
    public static void main(String[] args) {
         llenar();
         cdimensiones();
         mostrar();
    }
    public static void llenar(){
         int i;
         for(i=0; i<=4; i++){
           System.out.println("Ingrese el valor "+(i+1)+" del vector 1");
           vec1[i]=sc.nextInt();
         }

         for(i=0; i<=4; i++){
           System.out.println("Ingrese el valor "+(i+1)+" del vector 2");
           vec2[i]=sc.nextInt();
         }
         System.arraycopy(vec1, 0, aux1, 0, vec1.length);
         System.arraycopy(vec2, 0, aux2, 0, vec2.length);
    }
    public static void cdimensiones(){
       int i, j, sum=0, k;
       aux1=revrep(vec1);
       aux2=revrep(vec2);
    }
    public static int[] revrep(int array[]){
       int i, j;
       for(i=0; i<=3; i++){
          if(array[i]!=-1){
            for(j=(i+1); j<=4; j++){
                if(array[i]==array[j]){
                      array[j]=-1;
                }
             }
          }
       }

      return array;
    }
    public static void mostrar(){
       int i;
       System.out.print("\n");
       System.out.println("Auxiliar 1 es: ");
       for(i=0; i<=4; i++){
           System.out.print(aux1[i]+ " ");
       }
       System.out.print("\n");

       System.out.println("Vector 1 es: ");
       for(i=0; i<=4; i++){
           System.out.print(vec1[i]+ " ");
       }
       System.out.print("\n");

       System.out.println("Auxiliar 2 es: ");
       for(i=0; i<=4; i++){
           System.out.print(aux2[i]+ " ");
       }
       System.out.print("\n");

       System.out.println("Vector 2 es: ");
       for(i=0; i<=4; i++){
           System.out.print(vec2[i]+ " ");
       }
   }
} 
    
asked by Cat Shannon 16.11.2018 в 06:18
source

1 answer

1

you have the problem in this function

public static void cdimensiones(){
   int i, j, sum=0, k;
   aux1=revrep(vec1);
   aux2=revrep(vec2);
}

The function revrep receives a copy of the address pointed to by vec1, so if in this function you modify the array the modification is maintained and that is why you have this problem that both aux1 and vec1 are equal at the end.

I see that in the fill function you make a copy of vec1 and store it in aux1, so the solution is:

public static void cdimensiones(){
   int i, j, sum=0, k;
   aux1=revrep(aux1);
   aux2=revrep(aux1);
}

I hope it works for you.

    
answered by 16.11.2018 в 09:52