terça-feira, 22 de junho de 2021

C# - Jagged array de strings ( arrays irregulares )

 Postando exemplos práticos:


//Inicializando array jagged bidimensional de strings
using System;
using System.Collections;

class Arr {
    /*============================================================*/
    void Informe ( ) {
        Console.BackgroundColor = ConsoleColor.White;
        Console.SetCursorPosition ( 2016 );
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write ( "Por: " );
        Console.SetCursorPosition ( 2516 );
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.Write ( "Samuel Lima" );
        Console.SetCursorPosition ( 2017 );
        Console.ForegroundColor = ConsoleColor.Black;
        Console.Write ( "sa_sp10@hotmail.com" );
        Console.SetCursorPosition ( 3019 );
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write ( "MUITO OBRIGADO" );
    }
    /*============================================================*/
    void Moldura ( int tam_lin_iniint tam_lin_fimint tam_ini_col,
        int tam_fim_col ) {
        int ic;
        for ( i = tam_lin_inii < tam_lin_fimi++ ) {
            for ( c = tam_ini_colc < tam_fim_colc++ ) {
                Console.SetCursorPosition ( ci );
                Console.BackgroundColor = ConsoleColor.White;
                Console.Write ( " " );
            }
        }
    }
    /*============================================================*/
    void Ini_Array ( ) {
        int ij;
        //Inicializando array jagged bidimensional de strings
        /*
        //Assim abaixo, cada palavra ocupa uma coluna na matriz
        string [ ] [ ] texto = new string [ 12 ] [ ];
        texto [ 0 ] = new string [ 4 ] { "No", "Meio", "do", "Caminho" };
        texto [ 1 ] = new string [ 4 ] { "Carlos", "Drummond", "de", "Andrade" };
        texto [ 2 ] = new string [ 7 ] { "No", "meio", "do", "caminho", "tinha", "uma", "pedra" };
        texto [ 3 ] = new string [ 7 ] { "Tinha", "uma", "pedra", "no", "meio", "do", "caminho" };
        texto [ 4 ] = new string [ 3 ] { "Tinha", "uma", "pedra" };
        texto [ 5 ] = new string [ 7 ] { "No", "meio", "do", "caminho", "tinha", "uma", "pedra." };
        texto [ 6 ] = new string [ 5 ] { "Nunca", "me", "esquecerei", "desse", "acontecimento" };
        texto [ 7 ] = new string [ 7 ] { "Na", "vida", "de", "minhas", "retinas", "tao", "fatigadas." };
        texto [ 8 ] = new string [ 8 ] { "Nunca", "me", "esquecerei", "que", "no", "meio", "do", "caminho"};
        texto [ 9 ] = new string [ 3 ] { "Tinha", "uma", "pedra" };
        texto[ 10 ] = new string [ 7 ] { "Tinha", "uma", "pedra", "no", "meio", "do", "caminho" };
        texto[ 11 ] = new string [ 7 ] {"No", "meio", "do", "caminho", "tinha", "uma", "pedra."};
       
         */
        //Também pode ser inicialiado assim abaixo:
        //Assim abaixo, cada linha ocupa uma coluna na matriz
        /*
        string [ ] [ ] texto = {
        new string [ ] {"No Meio do Caminho                         "},
        new string [ ] {"Carlos Drummond de Andrade                 "},
        new string [ ] {"No meio do caminho tinha uma pedra         "},
        new string [ ] {"Tinha uma pedra no meio do caminho         "},
        new string [ ] {"Tinha uma pedra                            "},
        new string [ ] {"No meio do caminho tinha uma pedra.        "},
        new string [ ] {"Nunca me esquecerei desse acontecimento    "},
        new string [ ] {"Na vida de minhas retinas tao fatigadas.   "},
        new string [ ] {"Nunca me esquecerei que no meio do caminho "},
        new string [ ] {"Tinha uma pedra                            "},
        new string [ ] {"Tinha uma pedra no meio do caminho         "},
        new string [ ] {"No meio do caminho tinha uma pedra.        "}};
         */

        //Assim abaixo, cada linha ocupa uma coluna na matriz
        string [ ] [ ] texto = new string [ 12 ] [ ];
        texto [ 0 ] = new string [ 1 ] {"No Meio do Caminho                         "};
        texto [ 1 ] = new string [ 1 ] { "Carlos Drummond de Andrade                 " };
        texto [ 2 ] = new string [ 1 ] { "No meio do caminho tinha uma pedra         " };
        texto [ 3 ] = new string [ 1 ] { "Tinha uma pedra no meio do caminho         " };
        texto [ 4 ] = new string [ 1 ] { "Tinha uma pedra                            " };
        texto [ 5 ] = new string [ 1 ] { "No meio do caminho tinha uma pedra.  xx    " };
        texto [ 6 ] = new string [ 1 ] { "Nunca me esquecerei desse acontecimento    " };
        texto [ 7 ] = new string [ 1 ] { "Na vida de minhas retinas tao fatigadas.   " };
        texto [ 8 ] = new string [ 1 ] { "Nunca me esquecerei que no meio do caminho " };
        texto [ 9 ] = new string [ 1 ] { "Tinha uma pedra                            " };
        texto10 ] = new string [ 1 ] { "Tinha uma pedra no meio do caminho         " };
        texto11 ] = new string [ 1 ] { "No meio do caminho tinha uma pedra.        " };

        //Posiciona os textos no console
        Console.SetCursorPosition ( 214 );
        Console.ForegroundColor = ConsoleColor.Blue;
        for ( i = 0i < texto.Lengthi++ ) {
            for ( j = 0j < texto [ i ].Lengthj++ ) {
                Console.Write ( texto [ i ] [ j ] + " " );
            }
            Console.SetCursorPosition ( 21i + 5 );
        }
        Console.ReadKey ( );
        Console.WriteLine ( );
        Console.SetCursorPosition ( 2117 );
        Console.Write ( texto [ 5 ] [ 0 ] );//do
        Console.ReadKey ( );
    }
    /*============================================================*/
    public static void Main ( ) {
        Console.Title = ( "JAGGED ARRAY DE STRING" );
        Arr array = new Arr ( );
        array.Moldura ( 120268 );
        Console.SetCursorPosition ( 282 );
        Console.BackgroundColor = ConsoleColor.White;
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write ( "JAGGED ARRAY DE STRING" );
        //array.Informe ( );
        array.Ini_Array ( );
        //Segura a execução do programa
        Console.ReadKey ( );
    }
}
/*============================================================*/

Nenhum comentário:

Postar um comentário