如何将数组添加到队列中?

时间:2022-11-07 18:49:42

Is it possible to add an array into specifically, a ConcurrentLinkedQueue? If so, how exactly would the .add statement look like?

是否可以专门添加一个数组,一个ConcurrentLinkedQueue?如果是这样,.add语句到底会是什么样子?

THIS IS HOMEWORK RELATED, however, my entire program is meant to perform calculations on MyObjects (the default constructor of MyObjects generates random values to perform a ton of calculations on).

这是家庭作业相关,但是,我的整个程序是为了在MyObjects上执行计算(MyObjects的默认构造函数生成随机值以执行大量计算)。

For example:

//Note: I couldn't use the Diamond Braces for the Queue-type when posing this question

ConcurrentLinkedQueue theLinkedQueue {MyObjects[]} = 
                 new ConcurrentLinkedQueue{MyObjects[]}();

MyObjects[] theArray = null;

for(int i = 0; i < 100; i++){
   theArray = new MyObjects[CONSTANT_SIZE]; 
   theLinkedQueue.add(theArray(objparameter1, objparameter2));
}

The program implements multi-threading and in my thread class I've passed the Queue into the constructor, and am attempting to take off a MyObject array which a MyObject temp will point to, but so far I'm only capable of adding a single MyObject to my Queue at a time and pulling it. I want to be able to add the same amount of MyObjects as individual components rather than individually. I've attempted various lines of code only for NetBeans IDE to recommend a method to throw an UnsupportedOperation Exception. How could I add arrays into my ConcurrentLinkedQueue?

该程序实现了多线程,在我的线程类中,我已经将Queue传递给构造函数,并且我试图取出MyObject temp指向的MyObject数组,但到目前为止我只能添加一个MyObject一次到我的队列并拉动它。我希望能够将相同数量的MyObjects添加为单独的组件,而不是单独添加。我已尝试仅为NetBeans IDE使用各种代码行来推荐一种方法来抛出UnsupportedOperation异常。如何在ConcurrentLinkedQueue中添加数组?

(Also apologies if my question is dense or confusing, first time posting here).

(如果我的问题很密集或令人困惑,也要道歉,第一次在这里发帖)。

2 个解决方案

#1


1  

The correct syntax for the declaration of your queue is:

声明队列的正确语法是:

ConcurrentLinkedQueue<MyObjects> theLinkedQueue = new ConcurrentLinkedQueue<>();

Start with that and see how things go from there.

从那开始,看看事情是怎么发生的。

#2


0  

I figured out the solution, which was simply to add the array without 'objparameters' included.

我找到了解决方案,这只是添加数组而不包含'objparameters'。

theLinkedQueue.add(a);  //where a was a 'MyObject' array.

I presumed you had to load the parameter to be passed for each array index, which seems pretty silly.

我假设你必须为每个数组索引加载要传递的参数,这看起来很傻。

#1


1  

The correct syntax for the declaration of your queue is:

声明队列的正确语法是:

ConcurrentLinkedQueue<MyObjects> theLinkedQueue = new ConcurrentLinkedQueue<>();

Start with that and see how things go from there.

从那开始,看看事情是怎么发生的。

#2


0  

I figured out the solution, which was simply to add the array without 'objparameters' included.

我找到了解决方案,这只是添加数组而不包含'objparameters'。

theLinkedQueue.add(a);  //where a was a 'MyObject' array.

I presumed you had to load the parameter to be passed for each array index, which seems pretty silly.

我假设你必须为每个数组索引加载要传递的参数,这看起来很傻。