Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
Modify array with a method of the same class

i have class Deck

import java.util.Random;
public class Deck implements DeckActions{
    private static Card[] cards=new Card[52];
    public Deck () {
        int i=0;                                    
        for (Suit suit : Suit.values()){
            for (Rank rank : Rank.values()){
                cards[i++]=new Card(rank,suit); 
            }
        }
    }

    public Card draw() {                                 
        return cards[0];
    }

    public void shift(int times) {                          
        Random randomGenerator=new Random();
        int card1=randomGenerator.nextInt(52);
        int card2=randomGenerator.nextInt(52);

        for (int counter=0;counter < times; counter++){

        Card temp=new Card(null,null);
        temp=cards[card1];
        cards[card1]=cards[card2];
        cards[card2]=temp;
        }
    }
    public void show() {                            
        for (int i=0;i<cards.length;i++){
            System.out.println(cards[i].toString());    
        }   
    }
}

that implements interface

public interface DeckActions {

    public Card draw();                 
    public void shift(int times);       
    public void show();                 

}

the problem i have is that the Shift method doesn't actually change the deck array. What am i missing? I really can't figure out.

This is my main class, i tried to call the method with an int like 100, or 52, but show always shows the same array

public class TestCard {

    public static void main (String [] args) {
        Deck deck=new Deck();
            deck.show();
        deck.shift(52);
        System.out.println("Shifted");
        deck.show();

    }
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The problem is that lines which generate random indexes of cards to swap are outside loop, so all the time you swap the same 2 cards. Try:

 public void shift(int times) {                          
    Random randomGenerator=new Random();

    for (int counter=0;counter < times; counter++){

       int card1=randomGenerator.nextInt(52);
       int card2=randomGenerator.nextInt(52);

       Card temp=new Card(null,null);
       temp=cards[card1];
       cards[card1]=cards[card2];
       cards[card2]=temp;
    }
}

P.S: any reason for field cards to be static?

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda