在优先级队列中分配随机优先级?

Allocate random priority in priority queue?

我正在为 ServiceDesk 任务分配随机优先级(即高、中、低)。

在此之前,我想知道如何在所述优先级队列中存储(和打印)一个数组。这是我目前拥有的。

*更新代码

import java.util.*;
import java.util.Random;

public class Demo {

      public static void main (String[] args) {
          
      String [] names = {
         "Tanya Turner","Juana Downs","Mac Bautista","Leanne Donaldson",
         "Jere Romero","Autumn Hayden","Vincenzo Mckee","Penelope Stanley",
         "Rose Solis","Randal Savage","Delia Hardy","Alisha Hebert","Johnson Jefferson",
         "Gregorio Richard","Jana Simmons","Marian Shepherd","Lynn Pugh","Christine Newman",
         "Essie Frederick","Jewel Oneill","Raul Coleman","Lou Glover","Cora Rush",
         "Damien Norris","Omer Parsons","Adolph Petersen","Dane Proctor","Norbert Ritter",
         "Gloria Dickerson","Ella Morton","Madeline Mccullough","Patsy Berger","Tory Hardin",
         "Sonny Guzman","Kathrine Bond","Teodoro Bolton","Aimee Moran","Jerry Rhodes",
         "Palmer Golden","Zelma Hobbs","Marcella Patel","Freddy Lucas","Ladonna Hutchinson",
         "Devon Boone","Sue Graves","Chadwick Mcpherson","Antonia Rocha","Roseann Peters",
         "Leif Riggs","Judith Mcbride","Frances Simon","Nora Cervantes","Alba Hickman",
         "Concetta Wu","Chelsea Eaton","Dana Rocha","Hubert Kaiser","Phillip Stephenson",
         "Estela Kent","Rene Hughes","Clement Gilmore","Arlie Fernandez","Teodoro Buckley",
         "Daniel Chavez","Jeffry Shepherd","Devin Case","Eric Cooley","Dina Duncan","Teddy Price",
         "Matthew Cooke","Andres Dalton","Clayton Fields","Vito Lara","Lynette Mccann","Greta Choi",
         "Santo Noble","Thurman Douglas","Therese Norton","Juliette Graves","Fran Vang",
         "Forrest Gibbs","Cameron Bernard","Tracy Zhang","Hugh Huerta","Jaime Huynh","Tami Cordova",
         "Jami Mcpherson","Melissa Stein","Rayford Brewer","Tammie Lucero","Marcia Velez","Jasper Watkins",
         "Cora Chapman","Vickie Mccarthy","Gino Pena","Chadwick Hutchinson","Antonio Bryan",
         "Zachery Barnett","Randy Crawford","Laura Barton","Nolan Leach","Deborah Perry",
         "Georgina Sanford","Heidi Anthony","Leah Hester","Dong Swanson","Genevieve Wagner",
         "Russell Todd","Sherman Wolfe","Bo Schultz","Rosalyn Stevens","Brooke Moses","Jasmine Brock",
         "Guadalupe Andersen","Emilio Horne","Clara Spencer","Raul Levine","Colton Adams","Eve Avila",
         "Donny Murray","Laverne Valentine","Wilbert Gilbert","Justine Terrell","Waldo Nielsen",
         "Erma Mason","Brandie Sullivan","Murray Torres","Angelique Whitney","Shanna Humphrey",
         "Graig Farley","Lindsay Hines","Susanne Compton","Frankie Frank","Saundra Marks","Lorna Skinner",
         "Josephine Boyle","Maynard Wagner","Ronda Potts","Elias French","Gilberto Nguyen"
         };
      
         String[] priority = {"High", "Medium", "Low"};
         Random r = new Random();
         
         for (String i : names) {
            System.out.println("Names are: " + i);
            int randomPriority = r.nextInt(priority.length);
            System.out.println("Priority: " + priority[randomPriority]);
         }
            

       
               
   }
}

听起来您是在寻求有关如何开始的帮助。您正在寻求学习方面的帮助。以下是我将如何处理您的问题:

显然你应该使用优先级队列。

  1. 编写一个小程序,创建一个优先队列并将字符串存储到其中,然后将它们打印出来。
  2. 定义一个 class 并将该 class 的实例而不是字符串存储到优先级队列中。
  3. 修改优先级队列的排序标准,注意打印顺序根据排序标准发生变化。
  4. 编写一个函数,创建一个具有随机值的 class 实例。
  5. 编写一个函数来创建所有 100 个 class 个实例。
  6. 宣告胜利。

优先级队列本身不存储优先级。相反,它提供了一个钩子,询问“这个项目的优先级是什么?”并且您的代码会优先响应。因此,您有责任根据优先级来增加您存储的项目。

换句话说,您需要使用比纯字符串更丰富的数据格式。您可能应该并排存储名称和它们的优先级。它需要自定义 class,其中包含 authorpriority.

等字段
enum Priority {
    LOW,
    MEDIUM,
    HIGH
}

class Ticket {
    String author;
    String description;
    Priority priority;
}

您可以在创建每个 Ticket 时分配随机优先级。

绑定优先级队列和 Ticket class 的粘合剂是自定义比较器。使用 -> lambda 语法编写一个非常简单:

PriorityQueue<Ticket> queue = new PriorityQueue<>(
    Comparator.comparing(ticket -> ticket.priority)
);