Jump to content

Recommended Posts

Posted

HappyHippyHippo na parte com arraycopy não ta a funcionar bem.

corrigido:

import java.util.*;

/**
* Escreva um programa que leia uma série de palavras terminada por “fim” para
* um array. Posteriormente, o programa deverá ler mais uma série de palavras
* terminada por “ok”. O programa deverá verificar se a palavra inserida
* imediatamente antes da palavra “ok” existe no array e se existir deverá
* removê-la.
*/
public class WordSearch
{
   protected static final int ARRAY_SIZE = 1000;
   protected static final String FIRST_LIMIT = "fim";
   protected static final String SECOND_LIMIT = "ok";

   public static void show(String list[])
   {
       for (int i = 0; i < list.length; i++)
       {
           System.out.print(list[i] + " ");
       }
       System.out.println("");
   }

   public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);
       String word = "";

       System.out.println("Insira os elementos da primeira lista ('fim' para terminar) : ");
       String first[] = new String[ARRAY_SIZE];
       int first_size = 0;
       while (first_size != ARRAY_SIZE && !word.equals(FIRST_LIMIT))
       {
           word = sc.next();
           if (!word.equals(FIRST_LIMIT))
           {
               first[first_size] = word;
               first_size++;
           }
       }
       first = Arrays.copyOf(first, first_size);

       System.out.println("Insira os elementos da segunda lista ('ok' para terminar) : ");
       String second[] = new String[ARRAY_SIZE];
       int second_size = 0;
       while (second_size != ARRAY_SIZE && !word.equals(SECOND_LIMIT))
       {
           word = sc.next();
           if (!word.equals(SECOND_LIMIT))
           {
               second[second_size] = word;
               second_size++;
           }
       }
       second = Arrays.copyOf(second, second_size);

       Arrays.sort(second, new Comparator()
       {
           public int compare(Object s1, Object s2)
           {
               return ((String) s1).compareTo((String) s2);
           }
       });

       /* SOLUÇÃO SEM ARRAYCOPY */
       String result[] = new String[first.length];
       int result_size = 0;
       for (int i = 0; i < first.length; i++)
       {
           if (Arrays.binarySearch(second, first[i]) < 0)
           {
               result[result_size] = first[i];
               result_size++;
           }
       }
       result = Arrays.copyOf(result, result_size);

       System.out.println("Elementos não eliminados (SEM ARRAY COPY): ");
       show(result);

       /* SOLUÇÃO COM ARRAYCOPY */
       for (int i = 0; i < first.length; i++)
       {
           if (Arrays.binarySearch(second, first[i]) >= 0)
           {
               if (i < first.length - 1)
               {
                   System.arraycopy(first, i + 1, first, i, first.length - i - 1);
               }
               first = Arrays.copyOf(first, first.length - 1);

               i--;
           }
       }

       System.out.println("Elementos não eliminados (COM ARRAY COPY): ");
       show(first);
   }
}

Já agora se puderes podias explicar porque tornas a classe public?porque não private?

porque não ?

IRC : sim, é algo que ainda existe >> #p@p
Posted

porque não ?

Ok dá um erro...

Mas se puser sem o public o código é compilado na mesma.O que faz o public?

Se tiveres tempo mete nesse código comentários a descrever os métodos(construtor, selector, modicador...), classes, variáveis etc.

Obrigado

Posted

Eu já percebi o que o código faz. Eu não percebo é as razões de se usar (e quando usar) os public, private, procteted,..

Por exemplo quando declaraste a variável ARRAY_SIZE , colocaste protected static final. Isto significa que tu já não podes alterar mais o valor desta variável certo? Ou seja não poderias construir um método modificador para arraysize ...?

Posted (edited)

public : acessivel a todos os elementos da aplicação

protected : acessível somente à classe e suas derivadas

private : acessível somente à classe que declara

sim, ARRAY_SIZE é a maneira de declarar valores constantes em Java.

Edited by HappyHippyHippo
  • Vote 1
IRC : sim, é algo que ainda existe >> #p@p

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.