Java查看队列中的元素
所以我在做一个搜索算法。我正在使用队列存储我的所有对象 这就是我初始化它的方式Java查看队列中的元素,java,linked-list,queue,artificial-intelligence,greedy,Java,Linked List,Queue,Artificial Intelligence,Greedy,所以我在做一个搜索算法。我正在使用队列存储我的所有对象 这就是我初始化它的方式 Queue<Node> queue = new LinkedList<Node>(); Queue Queue=newlinkedlist(); 我想比较每个对象中的一个变量,并将其排序。我的计划是使用for循环将第一个对象与其他每个对象进行比较,并将变量最小的对象发送到队列的前面。然后移动到下一个对象并重复该过程。我的问题是,我不确定如何从队列中检索不是队列中第一个对象的对象……用于检索
Queue<Node> queue = new LinkedList<Node>();
Queue Queue=newlinkedlist();
我想比较每个对象中的一个变量,并将其排序。我的计划是使用for循环将第一个对象与其他每个对象进行比较,并将变量最小的对象发送到队列的前面。然后移动到下一个对象并重复该过程。我的问题是,我不确定如何从队列中检索不是队列中第一个对象的对象……用于检索对象而不删除它
一些示例代码:
import java.util.*;
class Example {
public static void main (String[] args) throws Exception {
Queue<String> list = new PriorityQueue<>();
{ // Initialize the Queue
list.add ("Hello ");
list.add ("Mrs. ");
list.add ("DoubtFire! ");
}
System.out.println (list);
// Iterating through the Queue
String element;
while ( (element = list.peek()) != null) {
if (element.equals ("Mrs. ")) {
System.out.println ("\"Mrs\" found!");
}
System.out.println (element);
list.remove (element);
}
System.out.println (list); // Empty by now...
}
}
用于检索对象而不删除它
一些示例代码:
import java.util.*;
class Example {
public static void main (String[] args) throws Exception {
Queue<String> list = new PriorityQueue<>();
{ // Initialize the Queue
list.add ("Hello ");
list.add ("Mrs. ");
list.add ("DoubtFire! ");
}
System.out.println (list);
// Iterating through the Queue
String element;
while ( (element = list.peek()) != null) {
if (element.equals ("Mrs. ")) {
System.out.println ("\"Mrs\" found!");
}
System.out.println (element);
list.remove (element);
}
System.out.println (list); // Empty by now...
}
}
队列接口不保证迭代或轮询时的任何特定顺序,因此理论上,此任务不可能通过队列实现 队列接口不保证迭代或轮询时的任何特定顺序,因此理论上,此任务不可能通过队列实现 您可以在队列中执行for循环:
for (Node n : queue) {
do stuff with n
}
但是,您将无法从队列中间删除项目。我可以建议使用类似ArrayList的结构吗?您可以在队列中执行for循环:
for (Node n : queue) {
do stuff with n
}
但是,您将无法从队列中间删除项目。我可以推荐一种类似ArrayList的结构吗?看到您对我的评论的回应,我认为您应该使用,因为它可以满足您的需要,而不需要您重新发明轮子,这通常是不推荐的 默认情况下,优先级队列将使用
compareTo
方法的默认实现。假设您有一个复合类型,则有两个选项:
您可以让自定义类实现该接口,并在其中设置排序逻辑
或者,您可以通过自己的比较器:
PriorityQueue<..> p = new PriorityQueue<..>(5, new Comparator<..>()
{
@override
public int compare(.. type1, .. type2)
{
//comparison logic done here.
}
}
PriorityQueue p=new PriorityQueue(5,new Comparator()
{
@凌驾
公共整数比较(…类型1,…类型2)
{
//比较逻辑在此完成。
}
}
您可以查看简短教程以了解更多信息。看到您对我的评论的回应,我认为您应该使用,因为它可以满足您的需要,而不需要您重新发明轮子,这通常是不推荐的 默认情况下,优先级队列将使用
compareTo
方法的默认实现。假设您有一个复合类型,您有两个选项:
您可以让自定义类实现该接口,并在其中设置排序逻辑
或者,您可以通过自己的比较器:
PriorityQueue<..> p = new PriorityQueue<..>(5, new Comparator<..>()
{
@override
public int compare(.. type1, .. type2)
{
//comparison logic done here.
}
}
PriorityQueue p=new PriorityQueue(5,new Comparator()
{
@凌驾
公共整数比较(…类型1,…类型2)
{
//比较逻辑在此完成。
}
}
您可以查看简短教程以了解更多信息。在我看来,最好的方法是使用。您可以指定接口的实现,该接口将强制规定元素在队列内部的排序方式 以下是一个例子: 假设这是您的节点类:
public class Node {
// this field will be used to sort in queue
private int value;
public Node(int value) {
this.value = value;
}
public int getValue() {
return value;
}
@Override
public String toString() {
return "My value is: " + value;
}
}
下面是向队列中添加节点的示例:
import java.util.PriorityQueue;
import java.util.Random;
public class QueueExample {
public static void main(String[] args) {
Random r = new Random();
// Priority queue with custom comparator
PriorityQueue<Node> queue = new PriorityQueue<Node>(10, new SampleNodeComparator());
// adding 100 nodes with random value
for(int i = 0; i < 100; ++i) {
queue.add( new Node(r.nextInt(1000)));
}
// nodes will be removed from queue in order given by comparator
while(queue.size() != 0) {
System.out.println(queue.remove());
}
}
}
import java.util.PriorityQueue;
导入java.util.Random;
公共类队列示例{
公共静态void main(字符串[]args){
随机r=新随机();
//具有自定义比较器的优先级队列
PriorityQueue队列=新的PriorityQueue(10,新的SampleNodeComparator());
//使用随机值添加100个节点
对于(int i=0;i<100;++i){
添加(新节点(r.nextInt(1000));
}
//节点将按比较器给定的顺序从队列中移除
while(queue.size()!=0){
System.out.println(queue.remove());
}
}
}
最重要的部分是我们定制比较器的实现
import java.util.Comparator;
// our comparator needs to implements Comparator interface
public class SampleNodeComparator implements Comparator<Node> {
@Override
public int compare(Node o1, Node o2) {
/*
value that should be return from compare method should follow rules:
if o1 == o2 - return 0
if o1 > o2 - return any positive value
if o1 < 02 - return any negative value
*/
return o1.getValue() - o2.getValue();
}
}
import java.util.Comparator;
//我们的比较器需要实现比较器接口
公共类SampleNodeComparator实现Comparator{
@凌驾
公共整数比较(节点o1、节点o2){
/*
应从比较方法返回的值应遵循以下规则:
如果o1==o2-返回0
如果o1>o2-返回任何正值
如果o1<02-返回任何负值
*/
返回o1.getValue()-o2.getValue();
}
}
当您从QueueExample类运行main方法时,您将在控制台上看到按Node.value值排序的值从队列中删除。在我看来,最好的方法是使用。您可以指定接口的实现,该接口将强制规定元素在队列内部的排序方式 以下是一个例子: 假设这是您的节点类:
public class Node {
// this field will be used to sort in queue
private int value;
public Node(int value) {
this.value = value;
}
public int getValue() {
return value;
}
@Override
public String toString() {
return "My value is: " + value;
}
}
下面是向队列中添加节点的示例:
import java.util.PriorityQueue;
import java.util.Random;
public class QueueExample {
public static void main(String[] args) {
Random r = new Random();
// Priority queue with custom comparator
PriorityQueue<Node> queue = new PriorityQueue<Node>(10, new SampleNodeComparator());
// adding 100 nodes with random value
for(int i = 0; i < 100; ++i) {
queue.add( new Node(r.nextInt(1000)));
}
// nodes will be removed from queue in order given by comparator
while(queue.size() != 0) {
System.out.println(queue.remove());
}
}
}
import java.util.PriorityQueue;
导入java.util.Random;
公共类队列示例{
公共静态void main(字符串[]args){
随机r=新随机();
//具有自定义比较器的优先级队列
PriorityQueue队列=新的PriorityQueue(10,新的SampleNodeComparator());
//使用随机值添加100个节点
对于(int i=0;i<100;++i){
添加(新节点(r.nextInt(1000));
}
//节点将按比较器给定的顺序从队列中移除
while(queue.size()!=0){
System.out.println(queue.remove());
}
}
}
最重要的部分是我们定制比较器的实现
import java.util.Comparator;
// our comparator needs to implements Comparator interface
public class SampleNodeComparator implements Comparator<Node> {
@Override
public int compare(Node o1, Node o2) {
/*
value that should be return from compare method should follow rules:
if o1 == o2 - return 0
if o1 > o2 - return any positive value
if o1 < 02 - return any negative value
*/
return o1.getValue() - o2.getValue();
}
}
import java.util.Comparator;
//我们的比较器需要实现比较器接口
公共类SampleNodeComparator实现Comparator{
@凌驾
公共整数比较(节点o1、节点o2){
/*
应从比较方法返回的值应遵循以下规则:
如果o1==o2-返回0
如果o1>o2-返回任何正值