【问题标题】:Java: Adding two random array elements for each category of a custom arraylistJava:为自定义数组列表的每个类别添加两个随机数组元素
【发布时间】:2016-10-15 09:09:14
【问题描述】:

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

我有一个用户偏好的字符串数组列表。

我还有一个包含数据库中所有视频的 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 循环或随机循环。我还看到猜测数字可能不是最好的解决方案记忆。所以我只是想确保我以最好的方式避免问题。

感谢您的帮助

【问题讨论】:

  • 对于 500 个视频和 5 个类别偏好,我希望它可以正常工作。在任何情况下,智者都会说,在您知道自己遇到性能问题之前不要尝试优化。
  • 很公平。谢谢
  • @NicholasMuir 如有任何疑问,请随意

标签: java arraylist random


【解决方案1】:

是的,您可以优化您的解决方案,如下所示:

目前while loop 的迭代被浪费了:假设categoryForRandomGuesscurrentCategoryPreference 都等于"Category 1"

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

我建议使用HashMap&lt;String,Integer&gt;,其中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));
      }
    } 

   }

【讨论】:

    猜你喜欢
    • 2014-06-19
    • 2022-07-11
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    相关资源
    最近更新 更多