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.
public class nombre_clase....
private int i;
El primer método es: VectorEntero.
- public void VectorEntero()
- { int num[]=new int [0];
- int num_tem[]=new int[0];
- int valor;
- int cadena;
- if(num.length==0)
- {num=new int[1];
- num[0]=Integer.parseInt (JOptionPane.showInputDialog ("Ingrese el valor " ));}
- else if(num.length>0)
- {num_tem=new int [num.length+1];
- for(int i=0;i<=(num.length-1);i++)
- {num_tem[i]=num[i];}
- num_tem[num_tem.length-1]=Integer.parseInt (JOptionPane.showInputDialog (null,"ingrese el valor " ));
- num=new int [num_tem.length];
- for(int i=0;i<=(num_tem.length-1);i++)
- {num[i]=num_tem[i];}
- }//fin else
- for(i=0;i<=(num.length-1);i++)
- {
- System.out.println (num[i] + " ");
- }
- }//fin metodo
El segundo método es: VectorReal()
- public void VectorReal()
- {
- double dato[]=new double [0];
- double dato_t[]=new double [0];
- int valor;
- int cadena;
- if(dato.length==0)
- {dato=new double [1];
- dato[0]=Integer.parseInt (JOptionPane.showInputDialog ("Ingrese el valor " ));}
- else if(dato.length>0)
- {dato_t=new double [dato.length+1];
- for(int i=0;i<=(dato.length-1);i++)
- {dato_t[i]=dato[i];}
- dato_t[dato_t.length-1]=Integer.parseInt (JOptionPane.showInputDialog (null,"ingrese el valor " ));
- dato=new double [dato_t.length];
- for(int i=0;i<=(dato_t.length-1);i++)
- {dato[i]=dato_t[i];}
- }//fin else
- for(i=0;i<=(dato.length-1);i++)
- {
- System.out.println (dato[i] + "");
- }
- }//fin metodo
Y por ultimo el método de tipo String: VectorTexto.
- public void VectorTexto()
- {
- String texto[]=new String [0];
- String texto_t[]=new String [0];
- if(texto.length==0)
- {texto=new String [1];
- texto[0]=JOptionPane.showInputDialog ("Ingrese el dato " );}
- else if(texto.length>0)
- {texto_t=new String [texto.length+1];
- for(int i=0;i<=(texto.length-1);i++)
- {texto_t[i]=texto[i];}
- texto_t[texto_t.length-1]=JOptionPane.showInputDialog (null,"ingrese el dato " );
- texto=new String [texto_t.length];
- for(int i=0;i<=(texto_t.length-1);i++)
- {texto[i]=texto_t[i];}
- } //fin else
- for(i=0;i<=(texto.length-1);i++)
- {
- System.out.println (texto[i] + " ");
- }
- }//fin metodo
0 comentarios: