viernes, 20 de junio de 2014

MODULO 5: Arrays



int b = {12,25,36,98,15}; // Error
int b[] = {12,25,36,98,15}; // correcto

Los arreglos tienen un numero definido


package EjAJ;

public class EjArrays {
    public static void main(String[] args) {
        int a[];
        a = new int[4];
        a[0] = 12;
        a[1] = 12;
        a[2] = 12;
        a[3] = 12;
       
      //  int b = {12,25,36,98,15}; //error
        int b[] = {12,25,36,98,15}; //bien
       
       
        Gato [] g = { new Gato("Don Gato"),  new Gato("Felix"),  new Gato("Gerrfiel")};
       
        for (int i = 0; i < g.length; i++) {
            Gato gato = g[i];
            System.out.println(gato.getNombre());
        }
       
        int k[], z, w;  //solo k es arreglo y z y w son enteros.
        int []f, o, c; // 3 arreglos de enteros.
       
        int j[][] = { {1,2,3}, {4,5,6,1}, {7,8,9},{7,2,3}};
        int []i[] = j;
        int [][]q;
       
        //las tres son correctas pero algunas no pueden tener más desclaraciones despues.7
       
        System.out.println(i[2][1]);
       
       
        int h[][] = new int[6][]; //6 posiciones (del indice 0 al 5).
        //int jk[][] = new int[][6];  //error.
        //en este casi SIEMPRE por lo menos debe tener el primero definido
       
       
       
    }
   
   
}

class Gato{

        private String nombre;

        public Gato(String nombre) {
            this.nombre = nombre;
        }
             
     
        public String getNombre() {
            return nombre;
        }

        public void setNombre(String nombre) {
            this.nombre = nombre;
        }

    }

No hay comentarios:

Publicar un comentario