Home > Mobile >  How can I convert a string into a Deque?
How can I convert a string into a Deque?

Time:10-11

Under class NewDeque I have a function public Deque<Character> IntoDeque(String a) where I have to take in an input as a String and the return value should be a Deque.

I'm struggling to understand what I have to write in order to achieve this. Here is my code so far:

public class NewDeque {
    public Deque<Character> IntoDeque(String a) {
        Deque<Character> L = new LinkedListDeque<>();
        for (int x = 0; x < a.length()-1; x  ) {
             L.add(a[x])   "";
        }
        return L;
    }
    }

I'm struggling to understand how to implement this as a Deque so, can anyone please help. Please provide explanations too since it would be good for me as well. Thank you.

Example input is "Side",

the output should be 's', 'i', 'd', 'e'

Here are some extra files in case you might want to know where I got certain methods from

LinkedListDeque (as of now):

public class LinkedListDeque<T> extends LinkedList<T> implements Deque<T> {

Interface Deque:

interface Deque<T> {
    void addFirst(T items);
    void addLast(T items);
    String toString();
    void removeLast();
    void removeFirst();
    void get(int index);
    int size();
    boolean isEmpty();
}

CodePudding user response:

You have used code to reference an array a[x] rather than character of the String which is a.charAt(x), and ignored last character of the string.

Deque<Character> dq = new LinkedList<>();
for (int x = 0; x < a.length(); x  ) {
    dq.add(a.charAt(x));
}
return dq;

It's not clear what you wanted to achieve by "", remove it.

  • Related