Ejemplos Arrays

  int[] numeros;

            // También se puede definir e inicializar en una sola línea
            String[] numeros2 = new String[5];
            foreach(String num in numeros2) {
                Console.WriteLine(num);
            }
            String[] alumnos = { "ana", "pep", "iu" };
            foreach (String alumno in alumnos)
            {
                Console.WriteLine(alumno);
            }
            for(int i = 0; i < alumnos.Length; i++)
            {
                Console.WriteLine("El alumno "+i+" es " + alumnos[i]);
            }
            alumnos[1] = "Pepito grillo";
            for (int i = 0; i < alumnos.Length; i++)
            {
                Console.WriteLine("El alumno " + i + " es " + alumnos[i]);
            }
            
            int[,] matriz = { { 1, 2, 3 }, { 4, 5, 6}, { 7, 8, 9 } };
            foreach (int num in matriz)
            {
                Console.WriteLine(num);
            }
            for(int i = 0; i < matriz.GetLength(0); i++)
            {
                for(int j = 0; j < matriz.GetLength(1); j++)
                {
                    Console.Write(matriz[i, j]+"  ");
                }
                Console.WriteLine("");
            }
            int[][] matriz2 = new int[2][];
            matriz2[0] =new int[]{ 1,2};
            matriz2[1]= new int[] { 1, 2,4,4,5,6,6 };
            String[] frase = { "hola", "que", "tal", "Estamos" };
            Console.WriteLine(String.Join(",",frase));
            Array.Sort(frase);
            Console.WriteLine(String.Join(",", frase));
            Array.Reverse(frase);
            Console.WriteLine(String.Join(",", frase));

Publicado por

Avatar del usuario

Juan Pablo Fuentes

Formador de programación y bases de datos