EJEMPLOS DE VECTORES MUESTRAN Y ALMACENAN DATOS
MOSTRAR EL VALOR DE CADA VECTOR
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pract1;
import java.util.Vector;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
*
* @author Rominita
*/
public class Pract1 {
public static int Leer(BufferedReader buff){
int lee=0; boolean error;
do{
error = false;
try{
lee = Integer.parseInt(buff.readLine());
}
catch (NumberFormatException ex){
System.out.println("se ha producido un error");
error = true;
}
catch (Exception ex){ex.printStackTrace(System.err);}
}while (error);
return lee;
}
//=====================================================
public static void cargar (int v []){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int i =0; i<v.length;i++){
System.out.print("Ingrese un valor:=");
v[i] = Leer(br);
}
}
//=============================================================
public static void Mostrar(int v[]){
for (int i=0; i<v.length;i++){
System.out.print("["+i+"]="+v[i]+"|");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int V1[]=new int [5];
cargar(V1);
Mostrar(V1);
}
}
ALMACENAR DATOS DE ESTUDIANTES EN UN VECTOR
package estudiante;
/**
*
* @author Rominita
*/
public class Estudiante {
private int RU;
private String Nombre;
private String Carrera;
public Estudiante(){
RU=0;
Nombre = "";
Carrera ="";
}
public Estudiante(int r,String n,String c){
RU = r;
Nombre = n;
Carrera = c;
}
public void setRU(int r){RU = r;}
public void setNombre(String n) {Nombre = n;}
public void setCarrera(String c){Carrera = c;}
public int getRU(){return RU;}
public String getNombre(){return Nombre;}
public String getCarrera(){return Carrera;}
public String VerEstudiante(){
return ("RU: "+RU+"\nNombre: "+Nombre+"\nCarrera: "+Carrera);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Estudiante E[] = new Estudiante[5];
E[0] = new Estudiante (2345,"Ana Gil","Derecho\n\n======");
E[1] = new Estudiante (654,"Juan Rocha","Pedagogia\n\n======");
E[2] = new Estudiante (987,"Pedro Paz","Derecho\n\n======");
E[3] = new Estudiante (6543,"Juana Arnez","Derecho\n\n======");
E[4] = new Estudiante (653,"Ryan Ortiz","Derecho\n\n======");
for(int i=0;i<E.length;i++){
System.out.println(E[i].VerEstudiante());
}
}
}
0 comentarios:
Publicar un comentario