_stan
Ominide
1 min. di lettura
Vota

Concetti Chiave

  • The method filters elements from array A that appear at most twice in array B and at least four times in array C.
  • A helper method is provided to check if an element is present at most twice in a given array.
  • A temporary array is initialized with the same size as array A to store potential results.
  • An index is used to iterate through the temporary array while processing elements from array A.
  • The example demonstrates that the element '4' from array A meets the specified conditions in arrays B and C.

Tema 32

Scrivere un metodo creaArrayConElementiDiApresentiMassimoDueVolteInBminimoQuattroIn che riceve in ingresso tre array di interi A, B, C e restituisce un array che contiene solo gli elementi di A presenti massimo due volte in B e minimo quattro volte in C.

Ad esempio, siano A, B, C i vettori cos costituiti

A
4 5 6 7 9
B
4 10 6 6 12 4 3 7
C
4 6 4 4 4
allora creaArrayConElementiDiApresentiMassimoDueVolteInBminimoQuattroInC( A, B, C) = 4

 public class tema32{ // creiamo un metodo di supporto per verificare se un elemento  presente al // massimo 2 volte in un array public static boolean isPresenteMassimoDueVolte (int p, int[] V) { boolean esito = false; int contaPresenze = 0; for (int i = 0; i = 4) // e in tal caso poniamo a TRUE la variabile boolean esito = true; } return esito; } // creaimo adesso il metodo finale public static int[] creaArrayConElementiDiApresentiMassimoDueVolteInBminimoQuattroInC (int[] A, int[] B, int[] C) { // approntiamo un array temporaneo della stessa dimensione di A // la scelta di tale dimensione  dovuta al fatto che tutti gli elementi di A // potrebbe soddisfare il requisito int[] arrayTemporaneo = new int[A.length]; // inizializziamo un indice per scorrere larray temporaneo int indice = 0; // iniziamo a scorrere larray da testare for (int i = 0; i    

Domande e risposte

Hai bisogno di aiuto?
Chiedi alla community