title: How to put a random number arraylist in an array?
body: <p>on the part of </p>
  
  <pre><code>if(boton == cartas[0]){
  
                      cartas[0].setLabel(figuras[Memorando.list.get(i)]);
                  }
  </code></pre>
  
  <p>in my code I'm trying to put a random number that I generated before but when the applet launches it gives me an error when I press the button, I want to put that code in all the buttons because I don't want repeated numbers, I want a randomly generated number for every button. I hope you can help me.</p>
  
  <pre><code>import javax.swing.*;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Stack;
  import java.awt.*;
  import java.applet.Applet;
  import java.awt.event.*;
  import java.util.*;
  public class Memorando extends Applet implements ActionListener {
      JButton cartas[];
      JButton juegoNuevo;
      Label marcador;
      public static ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
  
      public static ArrayList&lt;Integer&gt; random() {
          ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
          for (int i=0; i&lt;4; i++) {
              list.add(new Integer(i));
          }
          Collections.shuffle(list);
          for (int i=0; i&lt;4; i++) {
              System.out.println(list.get(i));
          }
          return list;
      }
  
      public void init(){
          this.setLayout(new BorderLayout());
          this.setBackground(Color.CYAN);
          Font appletFont=new Font("Monospased", Font.BOLD, 20);
          this.setFont(appletFont);
          juegoNuevo=new JButton("Juego nuevo");
          juegoNuevo.addActionListener(this);
          Panel topPanel=new Panel();
          topPanel.add(juegoNuevo);
          this.add(topPanel,"North");
          cartas=new JButton[8];
  
          Panel panelCentral=new Panel();
          panelCentral.setLayout(new GridLayout(2,4));
          this.add(panelCentral,"Center");
          marcador=new Label("No has ganado aun :(");
          this.add(marcador,"South");
          for(int i=0;i&lt;8;i++){
              cartas[i]=new JButton("*********");
              cartas[i].addActionListener(this);
          cartas[i].setBackground(Color.WHITE);
              panelCentral.add(cartas[i]);
          }
      }
  
  
  
      public void actionPerformed(ActionEvent e) {
  
  
          JButton boton=(JButton) e.getSource();
  
  
          String[] figuras=new String[8];
          figuras[0]="♥";
          figuras[1]="♣";
          figuras[2]="♠";
          figuras[3]="♦";
  
  
          if(boton == juegoNuevo){ 
              for(i=0;i&lt;8;i++){
  
              cartas[i].setEnabled(true); cartas[i].setLabel("*********");
                   cartas[i].setBackground(Color.CYAN);
                 }
  
              marcador.setText("Juego nuevo"); 
              juegoNuevo.setEnabled(false);
              return; 
  
          }
  
          if(boton == cartas[0]){
  
                      cartas[0].setLabel(figuras[Memorando.list.get(0)]);
                  }
  
                      if(boton == cartas[1]){
                          int numAleatorio=(int) (Math.random()*4);
                          cartas[1].setLabel(figuras[numAleatorio]);
                          }
                      if(boton == cartas[2]){
                          int numAleatorio=(int) (Math.random()*4);
                                  cartas[2].setLabel(figuras[numAleatorio]);
                              }
                      if(boton == cartas[3]){
                          int numAleatorio=(int) (Math.random()*4);
                                  cartas[3].setLabel(figuras[numAleatorio]);
                              }
                      if(boton == cartas[4]){
                          int numAleatorio=(int) (Math.random()*4);
                                  cartas[4].setLabel(figuras[numAleatorio]);
                              }
                      if(boton == cartas[5]){
                          int numAleatorio=(int) (Math.random()*4);
                                  cartas[5].setLabel(figuras[numAleatorio]);
                              }
                      if(boton == cartas[6]){
                          int numAleatorio=(int) (Math.random()*4);
                                  cartas[6].setLabel(figuras[numAleatorio]);
                              }
                      if(boton == cartas[7]){
                          int numAleatorio=(int) (Math.random()*4);
                                  cartas[7].setLabel(figuras[numAleatorio]);
                              }
      }
  
  
              void finDelJuego(){
                  juegoNuevo.setEnabled(true);
                  for(int i=0;i&lt;8;i++){
                      cartas[i].setEnabled(false);
                  }
              }
  
  
          }
  </code></pre>
  
  <p>The error I'm getting is 
  Exception in thread "AWT-EventQueue-1" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0</p>