1

I've got an ArrayList of Strings I need to use to set the text of a Label. So far I've tried many solutions but none seems to work.

I'd like to take a string randomly from the ArrayList to set my Label text and then remove this string from the ArrayList, so if I press a button that is located in the same panel, it will present a new string in the label but it never shows a string already shown.

This is what I've done so far.

I tried with remove method from List class, but it seems not to work.

package milionarioesame;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import javax.swing.JLabel;

/**
  * Descrive l'etichetta su cui verrà visualizzata la domanda.
 */
public class Question extends JLabel
{
Font cms = new Font("Comic Sans MS", 0, 30);

String d1 = new String("Alessandro Manzoni, noto");
String d2 = new String("Quale di queste città NON fa provincia nel Lazio?");
String d3 = new String("Se si moltiplicano fra di loro tutte le cifre che si trovano sul telefono fisso, si ottiene");
String d4 = new String("Quale di questi animali vive in acque marine?");
String d5 = new String("Cosa significa edulcorare?");
String d6 = new String("Quanti sono i cuccioli di dalmata rapiti in un famoso film Disney?");
String d7 = new String("Quale tra questi noti best-seller di Stephen King è ambientato in una prigione?");
String d8 = new String("Quale di questi pianeti è più vicino al Sole?");
String d9 = new String("Quale tra questi NON è il nome di una moneta?");
String d10 = new String("Quanti sono i magi che si recano a visitare Gesù bambino?");
String d11 = new String("Chi era la bella indiana dai capelli neri dell'omonimo film Disney?");
String d12 = new String("Se sentite tubare, affianco a voi avete");
String d13 = new String("Come si chiama il neonato reale d'Inghilterra?");
String d14 = new String("In quale Land della Germania si trova la città di Monaco?");
String d15 = new String("Tra queste squadre di calcio quale è l'unica ad avere sul suo stemma l'anno di fondazione? ");
String d16 = new String("Quale di questi musicisti NON ha mai fatto parte dei Queen?");
String d17 = new String("Com'è intitolato il settimo libro della celebre saga del mago Harry Potter?");
String d18 = new String("Qual è l'ultima nazione vincitrice del campionato del mondo di calcio?");
String d19 = new String("Sulla tavola degli elementi, \"Au\" indica"); // sequenza di escape
String d20 = new String("Se ammiro il mare da Piazza dell'Unità d'Italia, mi trovo a");

String[] aDomande1 = {d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15, d16, d17, d18, d19, d20};

ArrayList<String> domande1 = new ArrayList(Arrays.asList(aDomande1));

String dom = new String();
int idx = 0;

public Question()
{
    setFont(cms);
    setForeground(Color.WHITE);
    setHorizontalAlignment(JLabel.CENTER);
    setText(setDomanda());
}

public String setDomanda()
{
    Random rand = new Random();
    idx = rand.nextInt(domande1.size());
    dom = domande1.get(idx);
    return dom;
}

}

  • 3
    What happens when you try to remove a string from the `ArrayList`? What's wrong with simply adding `domande1.remove(idx)` after you get the value? – Flaom May 26 '17 at 09:04
  • Just to note, if your final project works with more value that those 20 `String`. I would change this logic to shuffle the `List` at the beginning into a `LinkedList`. then simply get the first value each time ( `list.remove(0)` ), this will transform a reading of O(n) into O(1) as you don't need to find a value in the middle of it. – AxelH May 26 '17 at 09:43
  • Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  Jun 05 '17 at 18:32
  • Please read [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) before attempting to ask more questions. –  Jun 05 '17 at 18:32

4 Answers4

1

Your code looks fine but I never see you actually removing anything. Try this version:

public String setDomanda() {
    Random rand = new Random();
    idx = rand.nextInt(domande1.size());
    return domande1.remove(idx);  // remove the string and return it
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
-1

user list iterator and use iteraotor.remove() instead of list.remove()

kma
  • 72
  • 1
  • 9
-1

Even simpler

public String setDomanda() {
    Random rand = new Random();
    idx = rand.nextInt(domande1.size());
    return domande1.remove(idx);
}

The remove(int index) method return the removed element.

-1

As an alternative, set up your list. Shuffle the list --Collections.shuffle(list) -- and then pick off the shuffled elements in order: 0, 1, 2, 3, ...

rossum
  • 15,344
  • 1
  • 24
  • 38