Java:为自定义arraylist的每个类别添加两个随机数组元素
我为用户提供了两个随机视频,用于他们选择的每个类别 我有一个用于用户首选项的字符串数组列表 我还拥有数据库中所有视频的ArrayList 因此,我试图一个接一个地搜索每个类别的偏好,并找到两个适合这些类别的随机视频 我已经实现了一个有效的解决方案(在一个小数据集上)。但考虑到不久将有500个视频、50个类别,用户将能够选择5个类别首选项,我不确定这是否是最佳方法: 我是这样计算出来的:Java:为自定义arraylist的每个类别添加两个随机数组元素,java,arraylist,random,Java,Arraylist,Random,我为用户提供了两个随机视频,用于他们选择的每个类别 我有一个用于用户首选项的字符串数组列表 我还拥有数据库中所有视频的ArrayList 因此,我试图一个接一个地搜索每个类别的偏好,并找到两个适合这些类别的随机视频 我已经实现了一个有效的解决方案(在一个小数据集上)。但考虑到不久将有500个视频、50个类别,用户将能够选择5个类别首选项,我不确定这是否是最佳方法: 我是这样计算出来的: //Create a new array to store the videos ArrayL
//Create a new array to store the videos
ArrayList<Video> videos = new ArrayList<>();
// Create counter for the position in the user preference array
int userPrefernceArrayIndex = 0;
// Create counter for number of successful category guesses
int numberOfSuccessfulGuesses = 0;
// Keep running until we have 2 videos for each of the user preferences
while (videos.size() < MainActivity.userPrefrencesStaticArraylist.size() * 2){
// Generate a random integer to get an entry random integer from the database array
Random rand = new Random();
int randomAlarmVidInt = rand.nextInt(MainActivity.allVideosFromDatabaseStaticArray.size());
// Find the category of the random video that was chosen
String categoryForRandomGuess = MainActivity.allVideosFromDatabaseStaticArray.get(randomAlarmVidInt).getVideoCategory();
// Find the current user video category we are testing for
String currentCategoryPreference = MainActivity.userPrefrencesStaticArraylist.get(userPrefernceArrayIndex);
// Check if category of the random video we got is the same as the category user
// preference we are testing for
if (categoryForRandomGuess.equals(currentCategoryPreference)){
// If it the the preference and the random video categories match add it to the video array
videos.add(MainActivity.allVideosFromDatabaseStaticArray.get(randomAlarmVidInt));
numberOfSuccessfulGuesses++;
// If the number of successful guesses is divisible by two then we have added two correct videos
// for that category so iterate to the next category
if (numberOfSuccessfulGuesses % 2 == 0){
userPrefernceArrayIndex++;
}
}
//创建一个新数组来存储视频
ArrayList视频=新建ArrayList();
//为用户首选项数组中的位置创建计数器
int userpreferencearrayindex=0;
//为成功的类别猜测次数创建计数器
int numberOfSuccessfulGuesses=0;
//继续运行,直到每个用户首选项都有2个视频
while(videos.size()
由于可能出现问题,除非必要,否则我几乎不会使用while循环或随机循环。我还发现,从内存角度来看,猜测数字可能不是最好的解决方案。因此,我只想确保我这样做是避免问题的最佳方式
感谢您的帮助是的,您可以优化您的解决方案如下所示: 目前,您的
while循环
的迭代次数正在浪费:比如说categoryForRandomGuess
和currentCategoryPreference
都等于“Category 1”
因此,numberOfSuccessfulGuess
变为1,但UserPreferenceArrayIndex
保持0
。因此,如果在下一次迭代中,categoryForRandomGuess
为“Category 4”
,即使currentCategoryPreference
可以变为“Category 4”,迭代也将被浪费
获取UserPreferenceArrayIndex的其他值,即0以外的值
我建议使用HashMap
,其中String
存储视频类别,Integer
存储在类别数据库中找到的第一个视频的索引
如果整数值
为-1,则意味着我们有2个视频,我们已经完成了该类别
现在,您可以消除变量userpreferencearrayindex
,代码将大大缩短
所以你的代码应该是:
HashMap<String,Integer> mymap = new HashMap<>();
while (videos.size() < MainActivity.userPrefrencesStaticArraylist.size() * 2)
{
// Generate a random integer to get an entry random integer from the database array
Random rand = new Random();
int randomAlarmVidInt = rand.nextInt(MainActivity.allVideosFromDatabaseStaticArray.size());
// Find the category of the random video that was chosen
String categoryForRandomGuess = MainActivity.allVideosFromDatabaseStaticArray.get(randomAlarmVidInt).getVideoCategory();
//check if the hashmap has the category
if(mymap.get(categoryForRandomGuess) == null)
{
mymap.put(categoryForRandomGuess,randomAlarmVidInt);
videos.add(MainActivity.allVideosFromDatabaseStaticArray.get(randomAlarmVidInt));
}
else
{//-1 means we already have 2 videos of the category.
if(mymap.get(categoryForRandomGuess) == -1)
continue;
//if following condition is true, then its a duplicate video
if(mymap.get(categoryForRandomGuess) == randomAlarmVidInt)
continue;//skip the rest of loop and get new value of randomAlarmVidInt
else
{
mymap.put(categoryForRandomGuess,-1);//second video added
videos.add(MainActivity.allVideosFromDatabaseStaticArray.get(randomAlarmVidInt));
}
}
}
HashMap mymap=newhashmap();
while(videos.size()
对于500个视频和5个类别的首选项,我希望它可以正常工作。无论如何,智者说,在你知道自己存在性能问题之前,不要尝试优化。这很公平。Thanks@NicholasMuir欢迎提出任何疑问