Método Vectores en java

Para este articulo utilizaremos el IDE Jcreator de java implementando tres métodos: Vector de enteros, Vector de reales y un ultimo vector de tipo String.
En un tutorial anterior les había presentado cada uno de los pasos a realizar para poder implementar esta clase de vectores, en donde el objetivo es dimensionar el mismo cada vez que se ingresa un nuevo dato.   

Miremos el funcionamiento del algoritmo, en le siguiente vídeo.

Lo primero es crear una clase, no importa el IDE que utilicemos. Luego declaramos una variable global para recorrer los vectores, ya que la utilizaremos en los tres métodos.
public class nombre_clase....
private int i;


El primer método es: VectorEntero.


  1. public  void VectorEntero()
  2.  {     int num[]=new int [0];
  3.     int num_tem[]=new int[0];
  4.        int valor;
  5.        int cadena;      
  6.      if(num.length==0)  
  7.      {num=new int[1];    
  8.      num[0]=Integer.parseInt (JOptionPane.showInputDialog  ("Ingrese el valor  " ));}    
  9.      else if(num.length>0)
  10.      {num_tem=new int [num.length+1];    
  11.      for(int i=0;i<=(num.length-1);i++)    
  12.      {num_tem[i]=num[i];}      
  13.      num_tem[num_tem.length-1]=Integer.parseInt (JOptionPane.showInputDialog  (null,"ingrese el valor  " ));
  14.      num=new int [num_tem.length];    
  15.      for(int i=0;i<=(num_tem.length-1);i++)
  16.      {num[i]=num_tem[i];}
  17.      }//fin else 
  18.      for(i=0;i<=(num.length-1);i++)
  19.      {
  20.      System.out.println (num[i] + "  ");
  21.     
  22.        }//fin metodo     
  23.             

           
El segundo método es: VectorReal()


  1. public  void VectorReal()
  2.         {        
  3.        double  dato[]=new double  [0];
  4.     double  dato_t[]=new double [0];
  5.        int valor;
  6.        int cadena;      
  7.      if(dato.length==0)  
  8.      {dato=new double [1];    
  9.      dato[0]=Integer.parseInt (JOptionPane.showInputDialog  ("Ingrese el valor  "  ));}    
  10.      else if(dato.length>0)
  11.      {dato_t=new double  [dato.length+1];    
  12.      for(int i=0;i<=(dato.length-1);i++)    
  13.      {dato_t[i]=dato[i];}      
  14.      dato_t[dato_t.length-1]=Integer.parseInt (JOptionPane.showInputDialog  (null,"ingrese el valor  " ));
  15.      dato=new double [dato_t.length];    
  16.      for(int i=0;i<=(dato_t.length-1);i++)
  17.      {dato[i]=dato_t[i];}
  18.      }//fin else 
  19.      for(i=0;i<=(dato.length-1);i++)
  20.      {
  21.      System.out.println (dato[i]  + "");
  22.     
  23.        }//fin metodo

     

Y por ultimo el método de tipo String: VectorTexto.



  1.  public  void VectorTexto()
  2.         {        
  3.        String   texto[]=new String  [0];
  4.     String texto_t[]=new String [0];      
  5.              
  6.      if(texto.length==0)  
  7.      {texto=new String [1];    
  8.      texto[0]=JOptionPane.showInputDialog  ("Ingrese el dato   "  );}    
  9.      else if(texto.length>0)
  10.      {texto_t=new String [texto.length+1];    
  11.      for(int i=0;i<=(texto.length-1);i++)    
  12.      {texto_t[i]=texto[i];}      
  13.      texto_t[texto_t.length-1]=JOptionPane.showInputDialog  (null,"ingrese el dato  " );
  14.      texto=new String [texto_t.length];    
  15.      for(int i=0;i<=(texto_t.length-1);i++)
  16.      {texto[i]=texto_t[i];}
  17.      }  //fin else
  18.     
  19.     
  20.      for(i=0;i<=(texto.length-1);i++)
  21.      {
  22.      System.out.println (texto[i]  + "  ");
  23.      }
  24.        }//fin metodo


0 comentarios: