Mostrando entradas con la etiqueta ejemplos. Mostrar todas las entradas
Mostrando entradas con la etiqueta ejemplos. Mostrar todas las entradas

lunes, 4 de octubre de 2010

UNA MATRIZ EN JAVA CON SCANNER


ESTA ES UNA SENCILLA AYUDA PARA QUIEN QUIERA EN JAVA LLENAR Y MOSTAR UNA MATRIZ, ADEMAS UTILIZO UN SCANNER PARA LEER DESDE LA CONSOLA.
PUEDEN CORTAR Y PEGAR EN SU PACKAGE Y FUNCIONARA SIN PROBLEMAS.
public class Main {
    public static Scanner EN = new Scanner(System.in);
    public static int i,j;
    public static void main(String[] args) {
        int M, N;
        System.out.println("inserte la cantidad de filas");
        M = EN.nextInt();//CON SCANNER JAVA  LEEMOS
        System.out.println("inserte la cantidad de columnas");
        N = EN.nextInt();//CON SCANNER JAVA  LEEMOS

        System.out.println("inserte los datos");

        int mat[][] = new int[M][N];//DECLARAMOS E INICIALIZAMOS UNA MATRIZ EN JAVA

        llenar(mat);//LLAMAMOS AL METODO LLENAR Y LE PASAMOS UNA MATRIZ EN JAVA
System.out.println("La Matriz queda asi!!");

        mostrar(mat);//LLAMAMOS AL METODO MOSTRAR Y LE PASAMOS UNA MATRIZ
    } 
//EL METODO EN JAVA PARA LLENAR
    private static void llenar(int[][] mat) {
       for ( i = 0; i < mat.length; i++) {
            for ( j = 0; j < mat.length; j++) {
                mat[i][j] = EN.nextInt();//EL OBJETO DE TIPO SCANNER LEE
            }
        }
    }
//EL METODO EN JAVA PARA MOSTRAR
    private static void mostrar(int[][] mat) {
        for ( i = 0; i < mat.length; i++) {
            for ( j = 0; j < mat.length; j++) {
                System.out.print(mat[i][j] + " ");
            }
            System.out.println();
        }
    }   
}
LUEGO DE INGRESAR LAS CANTIDADES PARA FILA Y COLUMNA, PEDIRA LOS DATOS
DE LA MATRIZ A LLENAR , POR ULTIMO MOSTRARA EN ORDEN Y EN FORMATO DE MATRIZ.
LA SALIDA POR CONSOLA DEBERA SER ASI:

viernes, 30 de julio de 2010

EJEMPLO DE ARRAYLIST

Creamos una clase con sus atributos privados
public class Producto{
private String nombre;
private int cantidad;
Cada uno con sus get and set
      private void setnombre (String nom){
            nombre=nom;
      }
     private void setnombre (int cant){
            cantidad=cant;

      }
     private String getnombre(){
            return nombre;
     }
      private String getnombre(){
            return nombre;
                                                                           }
Creamos los constructores el vacio y el sobrecargado
     public Producto{
     }
      public Producto(String nom,int cant){
              nombre=nom;
              canidad=cant;
     }
  }

En el Main hacemos lo siguiente

public class Main{                                          
     public void class main(String[] args){
Creamos 2 instancias de la clase Producto         
     Producto a = new Producto ("disco duro", 33256);
     Producto b = new Producto ("cable sata", 11656);
Creamos un ArrayList
      ArrayList lista = new ArrayList();
Insertamos los objetos
      lista.add(a);
      lista.add(b);
Creamos un metodo para mostrar
     mostrar(lista);


     }
Metodo para mostrar el ArrayList
      private void mostrar(List lista){
           for(iterator it = lista.iterator().hasNext()){
           Producto x = (Producto)it.Next();
           sout(x.getName+":"+x.getcantidad);
          }
      }
/*===============medicion============================*/