jeudi 31 août 2017

JAVA - Avoid duplication in String[]

the question is pretty common, but, because I'm a Java noob and I haven't much pratice with it, I couldn't applicate the case and the answers I found in other topic. So here's my case:

I want to have an ArrayList containing the file's name of a specified folder, and with this code I can have it:

File dir = new File(" ++++++ Insert Path Here ++++++");
        List<String> list = Arrays.asList(dir.list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".mp3");
            }
        }));

String[] stockArr = new String[list.size()];
        stockArr = list.toArray(stockArr);

After that, I want to name some JButtons with random Strings contained in stockArr, ok, pretty easy to do:

        JButton bottone1 = new JButton();
        JButton bottone2 = new JButton();
        JButton bottone3 = new JButton();

        Random r = new Random();

        bottone1.setText(stockArr[r.nextInt(stockArr.length)]);
        bottone2.setText(stockArr[r.nextInt(stockArr.length)]);
        bottone3.setText(stockArr[r.nextInt(stockArr.length)]);

Now my problem: How I can avoid to have duplicate random String in JButton's Text?

P.S.: I have to say that sometimes, searching for answer, I couldn't understand someone's suggestion beacause they write just a part of the code, or they assume that other person have the knowledge to understand at full what they write. So I'm asking if you please can be more specific at possible, step after step, just like tutorial, that will be very appreciate :)

Thanks in advance

The whole code:

import java.awt.Container;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class BottoniRandom {

    public static void main(String[] args) {

        JFrame finestra = new JFrame("ESEMPIO");
        finestra.setBounds(100, 500, 300, 200);

        Container contenuto = finestra.getContentPane();
        contenuto.setLayout(new BoxLayout(contenuto, BoxLayout.Y_AXIS));

        JButton bottone1 = new JButton();
        JButton bottone2 = new JButton();
        JButton bottone3 = new JButton();

        File dir = new File(" ++++++ Insert Path Here ++++++");
        List<String> list = Arrays.asList(dir.list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".mp3");
            }
        }));

        String[] stockArr = new String[list.size()];
        stockArr = list.toArray(stockArr);
        System.out.println(stockArr.length);
        Random r = new Random();

        bottone1.setText(stockArr[r.nextInt(stockArr.length)]);
        bottone2.setText(stockArr[r.nextInt(stockArr.length)]);
        bottone3.setText(stockArr[r.nextInt(stockArr.length)]);

        contenuto.add(bottone1);
        contenuto.add(bottone2);
        contenuto.add(bottone3);

        finestra.setVisible(true);
        finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}




Aucun commentaire:

Enregistrer un commentaire