mardi 30 juin 2015

Java: difference of first and last number in a deque [on hold]

I have to write a program which does the removal of numbers such that if the difference between the first number and the last number in the deque is even, the number at the head of the deque will be removed else if it is odd, the number at the tail will be removed. It should print out which number is being removed from the deque. I need some guidance in where I have gone wrong in my codes as I am getting exception error. Here is what I have come up so far.

package lesson1;
import java.util.*;

     public class MyClass1{

     public static void main(String[] args) {

     LinkedList<Integer> al= new LinkedList<Integer>();
     Deque<Integer> d = new LinkedList<Integer>();

        al.add(70);
        al.add(3);
        al.add(5);
        al.add(6);
        al.add(15);
        al.add(60);

        Integer first= al.getFirst();
        Integer last= al.getLast();

        Integer diff= first-last;
        Integer diff2= last-first;
        for(Integer i: al){

            if(diff%2==0|| diff2%2==0){

                d.removeFirst();
            }else{

                d.removeLast();
            }               
        }
        System.out.println(d);    
       }
     }

Aucun commentaire:

Enregistrer un commentaire