Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用数组+;奇偶数_Java_Arrays_Arraydeque - Fatal编程技术网

Java 使用数组+;奇偶数

Java 使用数组+;奇偶数,java,arrays,arraydeque,Java,Arrays,Arraydeque,我在编程练习中遇到了一些问题,我应该使用数组实现出列 我已经得到了我需要的操作,但是在实现之后,你应该运行数字1-20,并在出列的末尾插入偶数,奇数加在开头 之后,您应该使用方法removeFront删除列表中的所有数字,并在控制台上打印它们 还提示正确的输出是:(19,17,15…,1,2,4…,20) 我现在的问题是列表中缺少数字1,而是打印出一个空值作为第一个要删除的项 public class Dequeues<E> { private final int max; pri

我在编程练习中遇到了一些问题,我应该使用数组实现出列

我已经得到了我需要的操作,但是在实现之后,你应该运行数字1-20,并在出列的末尾插入偶数,奇数加在开头

之后,您应该使用方法removeFront删除列表中的所有数字,并在控制台上打印它们

还提示正确的输出是:(19,17,15…,1,2,4…,20)

我现在的问题是列表中缺少数字1,而是打印出一个空值作为第一个要删除的项

public class Dequeues<E> {

private final int max;
private int head;
private int tail;
private E[] deque;
private int counter;

public Dequeues(int max) {
    this.max = max;
    deque = (E[]) new Object[max];
    this.head = 0;
    this.tail = 0;
    this.counter = 0;
}

public boolean isEmpty (){
    return (counter == 0);
}

public boolean isFull() {
    return(counter>= max);
}

public void addFront (E x){
    if(!isFull()) {
        if (head == 0) {
            head = deque.length-1;
            deque[head] = x;
        } else {
            deque[head--] = x;
        }
        counter++;
    }
    else throw new IndexOutOfBoundsException("Stack is full!");
}

public void addBack(E x){
    if(!isFull()) {
        if(tail == deque.length-1) {
            tail = 0;
            deque[tail] = x;
        } else {
            deque[tail++] = x;
        }
        counter++;
    }
    else throw new IndexOutOfBoundsException("Stack is full!");
}

public E removeFront(){
    if(!isEmpty()) {
        E ret = deque[head];
        deque[head++] = null;
        if(head >= deque.length) {
            head = 0;
        }
        counter--;
        return ret;
    }
    else throw new IndexOutOfBoundsException("Stack is empty");
}

public E removeBack(){
    if (!isEmpty()) { 
        E ret = deque[tail]; 
        deque[tail--] = null;
        if(tail < 0) {
            tail = deque.length-1;
        }
        counter--;
        return ret;
        }
    else throw new IndexOutOfBoundsException("Stack is empty");
}


public static void main (String [] args) {
Dequeues test = new Dequeues(20);
for (int i = 1; i <= test.deque.length; i++) {
    if(i % 2 == 0) {
        test.addBack(i);
    } else if(i % 2 == 1) {
        test.addFront(i);
    }
}

System.out.println("Use of removeFront and output of the values: ");
for (int i = 0; i < test.deque.length; i++) {       
    System.out.print(test.removeFront() + " ");
    }   
}}
公共类出列{
私人最终整数最大值;
私人内部主管;
私人内特尾;
私人E[]德克;
专用int计数器;
公共出列(int max){
this.max=max;
deque=(E[])新对象[max];
这个头=0;
this.tail=0;
这个计数器=0;
}
公共布尔值为空(){
返回(计数器==0);
}
公共布尔值isFull(){
返回(计数器>=最大值);
}
公共空白地址(E x){
如果(!isFull()){
如果(头==0){
压头=deque.length-1;
德克[头]=x;
}否则{
德克[头--]=x;
}
计数器++;
}
否则抛出新的IndexOutOfBoundsException(“堆栈已满!”);
}
公共无效回拨(E x){
如果(!isFull()){
if(tail==deque.length-1){
尾=0;
deque[tail]=x;
}否则{
deque[tail++]=x;
}
计数器++;
}
否则抛出新的IndexOutOfBoundsException(“堆栈已满!”);
}
公共E-removeFront(){
如果(!isEmpty()){
E-ret=deque[头部];
deque[head++]=null;
如果(头部>=变形长度){
水头=0;
}
计数器--;
返回ret;
}
否则抛出新的IndexOutOfBoundsException(“堆栈为空”);
}
公共E removeBack(){
如果(!isEmpty()){
E-ret=deque[tail];
deque[tail--]=null;
如果(尾部<0){
尾翼=deque.length-1;
}
计数器--;
返回ret;
}
否则抛出新的IndexOutOfBoundsException(“堆栈为空”);
}
公共静态void main(字符串[]args){
出列测试=新出列(20);

对于(inti=1;i您只是错误地使用了--operator。 addFront方法的正确实现应该是:

public void addFront (E x){
    if(!isFull()) {
        if (head == 0) {
            head = deque.length-1;
            deque[head] = x;
        } else {
            deque[--head] = x;
        }
        counter++;
    }
    else throw new IndexOutOfBoundsException("Stack is full!");
}
因此,这里的差异是deque[--head]=x

--头意味着将头值减少一,然后使用它

head——指使用价值head,然后降低其价值

你的情况是:

  • 压头=deque.length-1;压头=19

  • head!=0,然后转到else语句。head值=19。使用head-,再次得到19,并将其减1,但必须使用--head


  • 啊,真管用,太谢谢你了!我没注意到。