sábado, 11 de fevereiro de 2017

ArrayList - Passagem como parâmetros

Mostrar como passar um arrayList como parâmetros
para uma função é o ponto principal deste programa.
O programa também mostra como um arrayList
pode ser inicializado, especialmente
quando utilizamos o método asList,
que retorna um ArrayList com os conteúdos de um array.
Neste exemplo mostramos também um claro exemplo de uso
do método shuffle, da classe Collections que é usado
para embaralhar os elementos de um arrayList.

Veja abaixo imagens do programa em execução:



Veja abaixo o código do programa:



import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
public class PROJETO {
     public static void imprime ( ArrayList < String > arrList ) {
         J jht = new J ( );
         int i;
        //////////////////////////////////////////////
         for ( i = 0; i < arrList.size ( ); i++ ) {
              if ( i == 0 )
                   jht.gotoxy ( ( int ) 25, ( int ) 8 );
              if ( i == 10 )
                   jht.gotoxy ( ( int ) 25, ( int ) 9 );
              if ( i == 20 )
                   jht.gotoxy ( ( int ) 25, ( int ) 10 );
              if ( i == 30 )
                   jht.gotoxy ( ( int ) 25, ( int ) 11 );
            if ( i == 40 )
                   jht.gotoxy ( ( int ) 25, ( int ) 12 );
              if ( i == 50 )
                   jht.gotoxy ( ( int ) 25, ( int ) 13 );
              if ( i == 60 )
                   jht.gotoxy ( ( int ) 25, ( int ) 14 );
              if ( i == 70 )
                   jht.gotoxy ( ( int ) 25, ( int ) 15 );
              if ( i == 80 )
                   jht.gotoxy ( ( int ) 25, ( int ) 16 );
              if ( i == 90 )
                   jht.gotoxy ( ( int ) 25, ( int ) 17 );
            System.out.print ( " " );
              System.out.print ( arrList.get ( i ) );
         }
         //////////////////////////////////////////////
     }
     public static void main ( String args [ ] ) {
         int i;
         J jht = new J ( );
         jht.title ( " " );
         jht.Moldura ( ( short ) 1, ( short ) 24, ( short ) 2, ( short ) 78 );
         ArrayList < String > arrList = new ArrayList <> ( Arrays.asList (
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
                   "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
                   "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33",
                   "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44",
                   "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55",
                   "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66",
                   "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77",
                   "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88",
                   "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100" ) );
         jht.textcolor ( jht.LIGHTRED );
         jht.gotoxy ( ( short ) 22, ( short ) 3 );
         System.out.println ( "ARRAYLIST - PASSANDO COMO PARÂMETROS" );
         jht.textcolor ( jht.LIGHTBLUE );
         jht.gotoxy ( ( short ) 25, ( short ) 5 );
         System.out.println ( "Veja abaixo os 100 elementos" );
         jht.gotoxy ( ( short ) 25, ( short ) 6 );
         System.out.println ( "inicializados pelo arrayList" );
         jht.textcolor ( jht.BLACK );
         imprime ( arrList );
         jht.textcolor ( jht.LIGHTMAGENTA );
         jht.gotoxy ( ( short ) 28, ( short ) 22 );
         System.out.println ( "Pressione qualquer tecla" );
         jht.getche ( );
         jht.cls ( );
         jht.textcolor ( jht.LIGHTRED );
         jht.gotoxy ( ( short ) 22, ( short ) 3 );
         System.out.println ( "ARRAYLIST - PASSANDO COMO PARÂMETROS" );
         jht.textcolor ( jht.LIGHTBLUE );
         jht.gotoxy ( ( short ) 27, ( short ) 5 );
         System.out.println ( "Veja abaixo os 100 elementos" );
         jht.gotoxy ( ( short ) 27, ( short ) 6 );
         System.out.println ( "do arrayList embaralhados" );
         jht.textcolor ( jht.BLACK );
         Collections.shuffle ( arrList );
         imprime ( arrList );
         jht.Sleep ( ( short ) 1800 );
         jht.textcolor ( jht.LIGHTRED );
         jht.gotoxy ( ( short ) 27, ( short ) 19 );
         System.out.printf ( "Por: " );
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf ( "Samuel Lima" );
         jht.textcolor ( jht.BLACK );
         jht.gotoxy ( ( short ) 27, ( short ) 20 );
         System.out.printf ( "sa_sp10@hotmail.com" );
         jht.Sleep ( ( short ) 1800 );
         jht.textcolor ( jht.LIGHTRED );
         jht.gotoxy ( ( short ) 37, ( short ) 22 );
         System.out.printf ( "MUITO OBRIGADO" );
         jht.getche ( );
     }
}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.