Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/01/2014, 09:25
SilverDante
 
Fecha de Ingreso: diciembre-2013
Mensajes: 36
Antigüedad: 10 años, 5 meses
Puntos: 1
Pregunta Copiar de un vector a otro solo números pares

Estoy haciendo un programa que copie los números pares de un vector a otro nuevo que contenga solo los valores pares del primero. El problema es que solo me copia el último valor parque encuentra.

Código:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package numparvecsolopar;

import java.util.*;

/**
 *
 * @author Joan
 */
public class NumParVecSoloPar {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        int [] v1;
        v1 = new int[6];
        
        introVector(v1);
        
        buscaPar(v1);
        
    }
    
    public static void introVector(int [] v){
    Scanner teclado=new Scanner (System.in);


    for(int i=0;i<v.length;i++){
    System.out.println("Introduce un valor para la posición "+i);
    v[i]=teclado.nextInt();
    }

}
    
    public static void buscaPar(int [] v){
        int rest, cont=0;

    for(int i=0;i<v.length;i++){
        int div=(int)v[i];
        
        rest=div%2;
        
        if(rest==0){
            cont+=1;
        }
    }
        
    //nuevo vector para los pares
    int [] vPar;
    vPar = new int[cont];
    
    for(int i=0;i<v.length;i++){
        int div=(int)v[i];
        
        rest=div%2;
            if(rest==0){
                for(int j=0;j<vPar.length;j++){
                    vPar[j]=(int)v[i];
                }
            }
    }
    
    for(int i=0;i<v.length;i++){
    System.out.print(v[i]); 
    }
    
        System.out.println("");
        
    for(int i=0;i<vPar.length;i++){
    System.out.print(vPar[i]); 
    }

}
    
}