I want to fetch only a single company name and I want to fetch it only once. So if it already was fetched, it should not be fetched again.
我想只获取一个公司名称,我只想获取一次。因此,如果已经获取它,则不应再次获取它。
Here is the code:
这是代码:
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;
}
2 个解决方案
#1
1
Make a copy in a List and remove the element when it was already fetched.
在List中创建一个副本,并在已经获取该元素时删除该元素。
Arrays.asList(array)
is not modifiable but you can wrap it in a full featured List.
Arrays.asList(array)不可修改,但您可以将其包装在功能齐全的List中。
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
#2
1
Just shuffle the array you want using Collections
只需使用Collections将您想要的数组随机播放
Collections.shuffle(List);
So simply create a list from your array
所以只需从数组中创建一个列表
List<E> list = Arrays.asList(array);
Then shuffle it using the method above
然后使用上面的方法将其洗牌
Collections.shuffle(list);
Your list can be read from left to right as it was random. So simply save the index
您的列表可以从左到右阅读,因为它是随机的。所以只需保存索引即可
int currentIndex = 0;
public E getRandom(){
//If at the end, start over
if(++currentIndex == list.size()) {
currentIndex = 0;
shuffle(list);
}
return list.get(currentIndex);
}
Each time you want to forget the duplicate list you already used, simply shuffle the array again
每次要忘记已经使用过的重复列表时,只需再次对阵列进行洗牌即可
Collections.shuffle(list);
Without index
You could simply remove the first value each time, once the list is empty, recreate it with the original array. As Ole V.V. pointer out, a List generated by Arrays.asList(E[])
doesn't support the remove methods so it is necessary to generate a new instance from it.
您可以每次删除第一个值,一旦列表为空,使用原始数组重新创建它。作为Ole V.V.指针输出,由Arrays.asList(E [])生成的List不支持remove方法,因此需要从中生成一个新实例。
Here is a quick and simple class using this solution :
以下是使用此解决方案的快速简单的类:
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);
}
}
And the test was done with this small code :
测试是用这个小代码完成的:
Integer[] array = {1,2,3,4,5};
RandomList<Integer> rl = new RandomList(array);
int i = 0;
while(i++ < 10)
System.out.println(rl.getRandom());
#1
1
Make a copy in a List and remove the element when it was already fetched.
在List中创建一个副本,并在已经获取该元素时删除该元素。
Arrays.asList(array)
is not modifiable but you can wrap it in a full featured List.
Arrays.asList(array)不可修改,但您可以将其包装在功能齐全的List中。
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
#2
1
Just shuffle the array you want using Collections
只需使用Collections将您想要的数组随机播放
Collections.shuffle(List);
So simply create a list from your array
所以只需从数组中创建一个列表
List<E> list = Arrays.asList(array);
Then shuffle it using the method above
然后使用上面的方法将其洗牌
Collections.shuffle(list);
Your list can be read from left to right as it was random. So simply save the index
您的列表可以从左到右阅读,因为它是随机的。所以只需保存索引即可
int currentIndex = 0;
public E getRandom(){
//If at the end, start over
if(++currentIndex == list.size()) {
currentIndex = 0;
shuffle(list);
}
return list.get(currentIndex);
}
Each time you want to forget the duplicate list you already used, simply shuffle the array again
每次要忘记已经使用过的重复列表时,只需再次对阵列进行洗牌即可
Collections.shuffle(list);
Without index
You could simply remove the first value each time, once the list is empty, recreate it with the original array. As Ole V.V. pointer out, a List generated by Arrays.asList(E[])
doesn't support the remove methods so it is necessary to generate a new instance from it.
您可以每次删除第一个值,一旦列表为空,使用原始数组重新创建它。作为Ole V.V.指针输出,由Arrays.asList(E [])生成的List不支持remove方法,因此需要从中生成一个新实例。
Here is a quick and simple class using this solution :
以下是使用此解决方案的快速简单的类:
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);
}
}
And the test was done with this small code :
测试是用这个小代码完成的:
Integer[] array = {1,2,3,4,5};
RandomList<Integer> rl = new RandomList(array);
int i = 0;
while(i++ < 10)
System.out.println(rl.getRandom());