Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayDeque add multiple elements

Im using arraydeque to create list of items and pass them parameters(Items is class)

ArrayDeque<Item> Items= new ArrayDeque<Item>();

But I have problem with java ArrayDeque. Maybe there are ways to add more than one element at once. For example. I want add at the same time TableType and colourOfTable into ArrayDeque.

In c++ I could have done it with this

vector<Item>Items

Items.push_back(Item("CoffeeTable", "brown"));

I want to do the same thing with Java. Instead of creating a new obj for every item, as:

ArrayDeque<Item> Items = new ArrayDeque<Item>();

Item obj = new Item("CoffeTable", "brown"); 
Items.add(obj);

Item obj1 = new Item("DinnerTable", "Black"); 
Items.add(obj1);

But instead of obj I want to add "CoffeTable", "brown" at the same time and with one code line (like in c++ example) into the Items array.

I tried something like that

ArrayDeque<Item> Items= new ArrayDeque<Item>();

Items.add(Items("CoffeTable", "brown")); 

But then got the error while creating create method 'Items(String,String)'

like image 552
Martynas Žukovas Avatar asked Jul 12 '13 08:07

Martynas Žukovas


People also ask

How do you add multiple elements to a queue in Java?

Instead of creating a new obj for every item, as: ArrayDeque<Item> Items = new ArrayDeque<Item>(); Item obj = new Item("CoffeTable", "brown"); Items. add(obj); Item obj1 = new Item("DinnerTable", "Black"); Items. add(obj1);

Is ArrayDeque faster than stack?

The ArrayDeque class implements the Deque interface This class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue.

Is ArrayDeque a stack?

An ArrayDeque (also known as an “Array Double Ended Queue”, pronounced as “ArrayDeck”) is a special kind of a growable array that allows us to add or remove an element from both sides. An ArrayDeque implementation can be used as a Stack (Last-In-First-Out) or a Queue(First-In-First-Out).


1 Answers

You can simple create the new item in the call of add:

items.add(new Item("CoffeTable", "brown"));

So you don't need an explicit variable.

Also note that in Java variable names normally start with a lower case character.

like image 52
Uwe Plonus Avatar answered Sep 17 '22 17:09

Uwe Plonus