Java 如何获得不重复的随机字符串值?

Java 如何获得不重复的随机字符串值?,java,arrays,random,Java,Arrays,Random,我只想取一个公司名称,而且只想取一次。因此,如果已提取,则不应再次提取 代码如下: private static String[] billercompanies = { "1st", "TELUS Communications", "Rogers Cablesystems", "Shaw Cable", "TELUS Mobility Inc", "Nanaimo Regional District of", "Credit

我只想取一个公司名称,而且只想取一次。因此,如果已提取,则不应再次提取

代码如下:

private static String[] billercompanies = { 
    "1st",      
    "TELUS Communications",
    "Rogers Cablesystems",
    "Shaw Cable",
    "TELUS Mobility Inc",
    "Nanaimo Regional District of",
    "Credit Union MasterCard",
    }; 



public static String GetBillerCompany(){
    String randomBillerComp = "";
    randomBillerComp = (billercompanies[new Random().nextInt(billercompanies.length)]);
    return randomBillerComp;
}

只需使用集合洗牌所需的数组

Collections.shuffle(List);
因此,只需从数组中创建一个列表

List<E> list = Arrays.asList(array);
您的列表可以从左到右阅读,因为它是随机的。 因此,只需保存索引即可

int currentIndex = 0;

public E getRandom(){
    //If at the end, start over
    if(++currentIndex == list.size()) { 
         currentIndex = 0;
         shuffle(list);
    }

    return list.get(currentIndex);
}
每次您想忘记已经使用的重复列表时,只需再次洗牌数组

Collections.shuffle(list);

无索引 您只需每次删除第一个值,一旦列表为空,就可以使用原始数组重新创建它。正如Ole V.V.所指出的,由
Arrays.asList(E[])
生成的列表不支持remove方法,因此需要从中生成新实例

下面是一个使用此解决方案的快速而简单的类:

public class RandomList<E>{
    E[] array;
    List<E> list;

    public RandomList(E[] array){
        this.array = array;
        buildList(array);
    }
    
    public E getRandom(){
        if(list.isEmpty()) buildList(array);
        return list.remove(0);
    }

    public void buildList(E[] array){
        list = new ArrayList<E>(Arrays.asList(array));
        Collections.shuffle(list);
    }
}
公共类随机列表{
E[]阵列;
名单;
公共随机列表(E[]数组){
this.array=数组;
构建列表(数组);
}
公共E getRandom(){
if(list.isEmpty())buildList(数组);
返回列表。删除(0);
}
公共void构建列表(E[]数组){
list=newarraylist(Arrays.asList(array));
集合。洗牌(列表);
}
}
测试是用这个小代码完成的:

Integer[] array = {1,2,3,4,5};
RandomList<Integer> rl = new RandomList(array);
int i = 0;
while(i++ < 10)
        System.out.println(rl.getRandom());
Integer[]数组={1,2,3,4,5};
随机列表rl=新的随机列表(数组);
int i=0;
而(i++<10)
System.out.println(rl.getRandom());

只需使用集合洗牌所需的数组即可

Collections.shuffle(List);
因此,只需从数组中创建一个列表

List<E> list = Arrays.asList(array);
您的列表可以从左到右阅读,因为它是随机的。 因此,只需保存索引即可

int currentIndex = 0;

public E getRandom(){
    //If at the end, start over
    if(++currentIndex == list.size()) { 
         currentIndex = 0;
         shuffle(list);
    }

    return list.get(currentIndex);
}
每次您想忘记已经使用的重复列表时,只需再次洗牌数组

Collections.shuffle(list);

无索引 您只需每次删除第一个值,一旦列表为空,就可以使用原始数组重新创建它。正如Ole V.V.所指出的,由
Arrays.asList(E[])
生成的列表不支持remove方法,因此需要从中生成新实例

下面是一个使用此解决方案的快速而简单的类:

public class RandomList<E>{
    E[] array;
    List<E> list;

    public RandomList(E[] array){
        this.array = array;
        buildList(array);
    }
    
    public E getRandom(){
        if(list.isEmpty()) buildList(array);
        return list.remove(0);
    }

    public void buildList(E[] array){
        list = new ArrayList<E>(Arrays.asList(array));
        Collections.shuffle(list);
    }
}
公共类随机列表{
E[]阵列;
名单;
公共随机列表(E[]数组){
this.array=数组;
构建列表(数组);
}
公共E getRandom(){
if(list.isEmpty())buildList(数组);
返回列表。删除(0);
}
公共void构建列表(E[]数组){
list=newarraylist(Arrays.asList(array));
集合。洗牌(列表);
}
}
测试是用这个小代码完成的:

Integer[] array = {1,2,3,4,5};
RandomList<Integer> rl = new RandomList(array);
int i = 0;
while(i++ < 10)
        System.out.println(rl.getRandom());
Integer[]数组={1,2,3,4,5};
随机列表rl=新的随机列表(数组);
int i=0;
而(i++<10)
System.out.println(rl.getRandom());

在列表中创建副本,并在已获取元素时将其删除

Arrays.asList(array)
不可修改,但您可以将其包装在功能齐全的列表中

List<String> billercompaniesList = new ArrayList<>(Arrays.asList(billercompanies));

String randomBillerComp = "";

Random random = new Random();
// first retrieval
int index = random.nextInt(billercompaniesList.size()); 
randomBillerComp = billercompaniesList.get(index);
billercompaniesList.remove(index);  


// second retrieval
index = random.nextInt(billercompaniesList.size()); 
randomBillerComp = billercompaniesList.get(index);
billercompaniesList.remove(index);  

// and so for    
List billercompaniesList=newarraylist(Arrays.asList(billercompanies));
字符串randomBillerComp=“”;
随机=新随机();
//首次检索
int index=random.nextInt(billercompaniesList.size());
randomBillerComp=billercompaniesList.get(索引);
billercompaniesList.remove(索引);
//二次检索
index=random.nextInt(billercompaniesList.size());
randomBillerComp=billercompaniesList.get(索引);
billercompaniesList.remove(索引);
//对我来说也是如此

在列表中创建副本,并在已获取元素时将其删除

Arrays.asList(array)
不可修改,但您可以将其包装在功能齐全的列表中

List<String> billercompaniesList = new ArrayList<>(Arrays.asList(billercompanies));

String randomBillerComp = "";

Random random = new Random();
// first retrieval
int index = random.nextInt(billercompaniesList.size()); 
randomBillerComp = billercompaniesList.get(index);
billercompaniesList.remove(index);  


// second retrieval
index = random.nextInt(billercompaniesList.size()); 
randomBillerComp = billercompaniesList.get(index);
billercompaniesList.remove(index);  

// and so for    
List billercompaniesList=newarraylist(Arrays.asList(billercompanies));
字符串randomBillerComp=“”;
随机=新随机();
//首次检索
int index=random.nextInt(billercompaniesList.size());
randomBillerComp=billercompaniesList.get(索引);
billercompaniesList.remove(索引);
//二次检索
index=random.nextInt(billercompaniesList.size());
randomBillerComp=billercompaniesList.get(索引);
billercompaniesList.remove(索引);
//对我来说也是如此

洗牌数组,然后按顺序迭代。当你想重新开始(忘记重复的部分)时,重新洗牌,就像@AxelH提到的那样。或者开始删除已经使用过的元素,并在该范围内生成一个新索引。但这比AxelH解决方案的性能成本要高一些。所以我更喜欢他的。可能重复的洗牌数组,然后按顺序迭代。当你想重新开始(忘记重复的部分)时,重新洗牌,就像@AxelH提到的那样。或者开始删除已经使用过的元素,并在该范围内生成一个新索引。但这比AxelH解决方案的性能成本要高一些。所以我更喜欢他的。你的可能复制品让我想到了我的第二个解决方案,使用remove:)我们不会重新发明轮子:)@AxelHI认为总的解决方案确实存在;)你让我想到了使用remove的第二个解决方案:)我们不会重新发明轮子:)@AxelHI我认为通用解决方案确实存在;)最后一个代码段不起作用。
Arrays.asList()
返回的列表大小固定(由数组支持),因此不支持
remove()
。我相信修复方法是
list=newarraylist(Arrays.asList(array))。另外,如果您不想使用原始的
列表
,请给它一个类型参数。@OleV.V。对,我记得这个。的确,这应该是可行的,但我会在一些测试后编辑它,但我暂时无法访问我的系统。对于原始类型,起初我试图避免使用类型,但忘记指定一个而不是对象。。。我对它进行了快速编辑,带有一些通用性。我将在运行一些测试(以及回家的时间)后的几个小时内再次编辑此内容。谢谢你在这方面的意见。@OleV.V。完成后,构造函数中的列表副本就足够了。记住这一点总是很好的。最后一个片段不起作用。
数组返回的列表