6

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)'

Marc von Renteln
  • 1,229
  • 15
  • 34
Martynas Žukovas
  • 149
  • 1
  • 5
  • 13

4 Answers4

4

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.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
2

You will have to create a new object anyway to hold these 2 values. You can do this:

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

Anything else you come up with will be syntactic sugar for the above

For example: you can add a static method to your class:

public static Item item(String k1, String k2) { 
    return new Item(k1, k2);
}

And use it later:

Items.add(item("CoffeTable", "Brown"));
rahulserver
  • 10,411
  • 24
  • 90
  • 164
Tala
  • 8,888
  • 5
  • 34
  • 38
1

Here is a solution which would surely work. You can add a function to your class itemAdd() as follows:

class Samp {
    public static void main(String args[]){
        //code.....
        ArrayDeque<Item> Items= new ArrayDeque<Item>();
        Items.add(itemAdd("CoffeeTable", "brown"));
        //rest of code....
    }
    public static Item itemAdd(String tableType,String colourOfTable){
        return new Item(tableType,colourOfTable);
    }
}
class Item{
    String tableType;
    String colourOfTable;
    Item(String tableType,String colourOfTable ){
        this.tableType=tableType;
        this.colourOfTable=colourOfTable;
    }
}

Its similar to what u need to do!! Best of luck :)

rahulserver
  • 10,411
  • 24
  • 90
  • 164
0

For input multiple values in Queue use this:

Queue<Integer> q1 = new ArrayDeque<>(Arrays.asList(1,2,3,4,5,6,7,8,9,10));

You can also use LinkedList instead of ArrayDeque.

With this command you can simply put multiple values in a Queue/Linkedlist.

borchvm
  • 3,533
  • 16
  • 44
  • 45
  • This is a 10 year old question with an accepted answer; there is rarely a point in adding another one. – daniu Mar 09 '23 at 09:47