-2
Deque<String> d = new Deque<String>();

I want to change that later in the code to something like:

d = new Deque<Integer>();

Whenever I do it like that I get:

incompatible types Deque<Integer> cannot be converted to Deque<String>

How can I make it work, so I can assign d to Deque<Integer>?

I'm trying to accomplish the following:

Deque<String> d = new Deque<String>();
d.pushLeft("Datastructuren");
d.pushLeft("is");
d.pushRight("heel");
d.pushRight("leuk");
d.pushLeft("of");
d.pushRight("niet?");
d.changeLeft(5, "test");
d.changeRight(3, "een");
d.changeLeft(2, "Dit");
d.changeRight(4, "is");
d.popRight();
d.popLeft();

for (String i : d) {
    System.out.print(i + " ");
}

d = new Deque<Integer>();
d.pushLeft(67);
d.pushLeft(1);
d.pushLeft(13);
d.pushRight(18);
d.pushRight(10);
d.pushRight(2);
d.pushLeft(29);
d.pushRight(88);
d.changeLeft(5, 25);
d.changeRight(1, 17);
d.popRight();
d.changeLeft(8, 18);
d.changeRight(5, 19);
d.popLeft();

for (int i : d) {
    System.out.print(i + " ");
}

I know I can make a different variable but I was just curious if I could change the current d variable so it would use Deque<Integer>.

Drago
  • 1,755
  • 3
  • 19
  • 30
  • 3
    Why do you want to do something like this? You would concretely type the deque *before* you use it, not *while* you're using it. – Makoto Sep 30 '16 at 15:09
  • Why declare a variable of type `Deque` if you need to set it to a `new Deque()` ? – khelwood Sep 30 '16 at 15:11
  • You can't. You declared d as a `Deque`. That's the type it **is**. If you want to have a value pointing to a `Deque`, you'll have to declare a new, different variable that is the correct type. – Frecklefoot Sep 30 '16 at 15:11
  • just leave off the explicit and leave it as Object, that way you can cast later. – scarecrow- Sep 30 '16 at 15:12
  • 2
    @scarecrow-: That's a horrible idea. Use strong typing when appropriate. The question becomes, *why* the OP thinks this is appropriate. – Makoto Sep 30 '16 at 15:12
  • Edited the question. – Drago Sep 30 '16 at 15:13
  • @Makoto OP asked how to do something, not if it was appropriate to do it. – scarecrow- Sep 30 '16 at 15:14
  • Java generics are part of the declaration and checked by the compiler, start with a tutorial first? http://thegreyblog.blogspot.de/2011/03/java-generics-tutorial-part-i-basics.html – Arne Burmeister Sep 30 '16 at 15:15
  • 2
    @scarecrow- but the right way is *not* to leave off the ``, it is to make the bound ``. http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – Andy Turner Sep 30 '16 at 15:15
  • What kind of deque object is this? As written, this isn't one of the standard collections. – Makoto Sep 30 '16 at 15:21
  • Nvm, I get it now. I was confused because of the loosely typed languages. – Drago Sep 30 '16 at 15:21
  • @Makoto it's my own implementation of deque. `public class Deque implements Iterable `. – Drago Sep 30 '16 at 15:22

4 Answers4

0

The only realistic way this could be accomplished is if your collection has a lower bound of Object.

Deque<? super Object> d = new Deque<>();

The advantage of this is that you can do what you want without compromising type safety. The disadvantage of this is that you won't be able to extract a value from d other than Object.

// Possible
d.pushLeft("Datastructuren");

// Not type safe in the least
String first = d.get(0);

As a comparison, if we were to use a vanilla collection like List, we could accomplish this:

List<? super Object> foo = new ArrayList<>();

foo.add(1);
foo.add(2);
foo.add("cabbage");

for(Object o : foo) {
    System.out.println(o);
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • You cannot apply methods adding an element on that as the compiler has no chance to check the type is correct. – Arne Burmeister Sep 30 '16 at 15:33
  • @ArneBurmeister: This works just fine with other data structures. I'll make that explicit in an edit. – Makoto Sep 30 '16 at 15:35
  • Argh, was confused, thought about `? extends Object`. This works but is at the end the same as `Deque` as there is no possible super type of `Object`. – Arne Burmeister Sep 30 '16 at 15:37
0

You should not try to give a (useless) solution to a question asking for something a programming language is not made for. In this case the answer is simply No way!

The real question is: Why do you want to do so?

  • Instead you can simply use another variable.
  • If you just want to fill the queue type safe but read regardless of the type you can refactor like below

At the end it is not really clear what you want to do with this code. If it just learning how a Deque works than a 2nd variable would do the job best. If not, refactoring would be an idea and maybe generic methods would help in processing the queue later.

Deque<?> d = fillWithString();

for (Object i : d) {
    System.out.print(i + " ");
}

d = fillWithInt();

for (Object i : d) {
    System.out.print(i + " ");
}

private Deque<String> fillWithString() {
  Deque<String> d = new Deque<String>();
  d.pushLeft("Datastructuren");
  d.pushLeft("is");
  d.pushRight("heel");
  d.pushRight("leuk");
  d.pushLeft("of");
  d.pushRight("niet?");
  d.changeLeft(5, "test");
  d.changeRight(3, "een");
  d.changeLeft(2, "Dit");
  d.changeRight(4, "is");
  d.popRight();
  d.popLeft();
  return d;
}

private Deque<Integer> fillWithInt() {
  Deque<Integer> d = new Deque<Integer>();
  d.pushLeft(67);
  d.pushLeft(1);
  d.pushLeft(13);
  d.pushRight(18);
  d.pushRight(10);
  d.pushRight(2);
  d.pushLeft(29);
  d.pushRight(88);
  d.changeLeft(5, 25);
  d.changeRight(1, 17);
  d.popRight();
  d.changeLeft(8, 18);
  d.changeRight(5, 19);
  d.popLeft();
  return d;
}

You cannot add or replace elements in d outside the fill methods in this refactoring as the compiler cannot check the type any more.

Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94
-1

Declare d as Deque<Object> so it can be assigned to any instance. You have to take care about de referred type of each instance with instanceof for each instance to meke special uses of them.

Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94
Francisco Valle
  • 613
  • 10
  • 10
-3

It is not possilbe. You can try this:

Deque d = new Deque<String>();