简体   繁体   English

将一个对象数据移动到另一个

[英]Moving one object data to another

So basically, I want to move an object data to another object (Queue jockeying). 因此,基本上,我想将一个对象数据移动到另一个对象(队列骑战)。

let's say I have 假设我有

int counter1,counter2

and I have methods 我有方法

void enQueue1(object data)
void enQueue2(object data)
void deQueue1()
void deQueue2()

And, for example, I do this 而且,例如,我这样做

obj1.enQueue1("data 1")
obj2.enQueue2("data 2")
obj2.enQueue2("data 3")
obj2.enQueue2("data 4")

What I wanna do is when the difference between counter1 and counter2 is >1, I want to move the data from larger counter so I have a balanced counter1 and counter2 我想做的是当counter1和counter2之差> 1时,我想从较大的counter中移动数据,所以我有一个counter1和counter2平衡的

So, how can I move the "data 4" from obj2 to obj1? 那么,如何将“数据4”从obj2移动到obj1?

I wrote a pseudocode that look like this: 我写了一个伪代码,看起来像这样:

if (counter1 - counter2) == -2
obj2.deQueue2
obj1.enQueue(obj2.enQueue("data 4"))

How can I achieve this in java code? 如何在Java代码中实现这一目标? Your help is much appreciated 非常感谢您的帮助

Thanks! 谢谢!

Have your deQueue methods return the last inserted object, and remove it from the current class as you probably already do : 让您的deQueue方法返回最后插入的对象,然后像您可能已经将其从当前类中删除:

Object deQueue()

Then 然后

obj1.enQueue(obj2.deQueue());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM