Java:为自定义数组列表的每个类别添加两个随机数组元素

标签 java arraylist random

我为用户选择的每个类别提供两个随机视频。

我有一个用于用户首选项的字符串 ArrayList。

我还有一个包含数据库中所有视频的 ArrayList。

因此,我尝试逐一搜索每个类别偏好,并找到两个适合该类别的随机视频。

我已经实现了一个有效的解决方案(在小数据集上)。但考虑到很快就会有 500 个视频、50 个类别,并且用户将能够选择 5 个类别首选项,我不确定这是否是最佳方法:

这就是我的解决方法:

    //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++;
            }
        }

由于出现问题的可能性,除非必要,否则我几乎不会使用 while 循环或随机循环。我还发现猜测数字可能不是内存方面的最佳解决方案。所以我只是想确保我正在以最好的方式避免出现问题。

感谢您的帮助

最佳答案

是的,您可以按如下方式优化您的解决方案:

目前您的 while loop 的迭代被浪费了:说categoryForRandomGuesscurrentCategoryPreference两者都等于 "Category 1" .

所以numberOfSuccessfulGuesses变为 1,但是 userPrefernceArrayIndex停留0 。因此,如果在下一次迭代中,如果 categoryForRandomGuess"Category 4" ,即使currentCategoryPreference,迭代也会被浪费。可以等于 "Category 4" 对于 userPrefernceArrayIndex 的其他值,即 0 以外的值

我建议使用 HashMap<String,Integer>哪里String存储视频类别和 Integer存储在该类别的数据库中找到的第一个视频的索引。

如果Integer value为 -1,这意味着我们有 2 个视频,并且我们已经完成了该类别。

现在您可以消除变量 userPrefernceArrayIndex你的代码会短很多

所以你的代码是:

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));
      }
    } 

   }

关于Java:为自定义数组列表的每个类别添加两个随机数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40057186/

相关文章:

java - 对所有元素求和 java arraylist

random - 如何生成错误的随机数

java - 使用 JUnit 理论时期望异常

java - 使用 Java GDAL API 进行扭曲

java - native 库 sqljdbc_auth.dll 已在另一个类加载器中加载

java - 使用ArrayList从数据库中检索数据并将其显示在JSP中

java - java中不可阻塞的方法

java - Arraylist.get 返回 null

c++ - C/C++的rand()怎么能这么快生成随机数呢?

c++ - 每次循环时都会创建相同的随机数。