K
K
keddad2020-04-17 11:56:37
Java
keddad, 2020-04-17 11:56:37

Why can't an ArrayDeque be created for Chars?

Let's say I create an ArrayDeque for chars:

ArrayDeque<char> pass_raw = new ArrayDeque<char>();

Or I make a function that accepts it:
static public void stackUpper(ArrayDeque<char> pass) {
        if (pass.getLast() == 'A') {
            pass.addLast('B');
        } else {
            pass.addLast('A');
        }
    }


But it doesn't work! IntelliJ does not swear, but the compiler says that it expected a reference (to what?) instead of a char:
Main.java:5: error: unexpected type
    public static void stackUpper(ArrayDeque<char> pass) {
                                             ^
  required: reference
  found:    char

Main.java:39: error: unexpected type
        ArrayDeque<char> pass_raw = new ArrayDeque<char>();
                   ^
  required: reference
  found:    char
Main.java:39: error: unexpected type
        ArrayDeque<char> pass_raw = new ArrayDeque<char>();
                                                   ^
  required: reference
  found:    char

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Pokrovsky, 2020-04-17
@keddad

In java, generics cannot use primitives. There are wrapper classes for this. For char it's Character, for int it's Integer, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question