如何在不使用Stack的字符的ArrayList中读取数字

时间:2015-04-17 14:35:53

标签: java arraylist

  

我正在创建一个CharacterList of Characters来读取我输入的每一个。如果我的输入为0-9,我的ctr将为+1,当我的输入为运算符时,我的ctr将为-1,但我如何才能将此数字(例如12)作为一个语句读取?我能得到什么最好的选择?

     

继承我的守则:

  class Validation2
{
 static Scanner input=new Scanner(System.in);
 static String confirmation = null;
public static void main(String args[])
{

    int ctr=0;
    ArrayList<Character> list=new ArrayList<Character>();
    String notation;


    do
    {

        System.out.println("Enter Postfix Notation: ");
        notation =input.nextLine();

    if(notation.matches("[A-Za-z]") || notation.matches("[\\p{Punct}&&[^_]]+"))
    {
        System.out.println("Invalid Input sorry.");
    }
    else if(notation.charAt(0) <= 0 || notation.charAt(1) >= 9)
    {
     for(int x = 0; x < notation.length(); x++)
     {
        list.add(notation.charAt(x));

        if(list.get(x).equals('/') || list.get(x).equals('*')||list.get(x).equals('-')|| list.get(x).equals('+'))
        {
                 ctr--;
                 System.out.println(list.get(x));
        }
         else if(list.get(x) <= 0 || list.get(x) >= 9)
                 ctr++;

         }
     }
    else if(notation.charAt(0) == '/' || notation.charAt(0) == '*' || notation.charAt(0) == '-' || notation.charAt(0) == '+' ||
            notation.charAt(1) == '/' || notation.charAt(1) == '*' || notation.charAt(1) == '-' || notation.charAt(1) == '+')
            System.out.println("The first two numbers should be operands");

    if(ctr == 1)
    {
        System.out.println("The post fix is valid");

    }
    else
    {
        System.out.println("The post fix is invalid");
    }

    System.out.println(ctr);
    ctr = 0;
    list.clear();
    System.out.println("Do you want to Continue? Y/N");
    confirmation = input.nextLine().toUpperCase();



     }while(confirmation.equals("Y"));


}

2 个答案:

答案 0 :(得分:1)

不要使用数组列表进行后缀表示法。使用简单的堆栈并相应地删除操作员的数字。可以使用简单的Deque(例如LinkedList)来实现堆栈。

这是一个示例实现:

package tk.manf.util.collection;

import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * LiFo implementation using a LinkedList
 * @author Björn 'manf' Heinrichs
 */
public class Stack<V> implements Iterable<V> {
    private final Deque<V> data;

    public Stack() {
        this.data = new LinkedList<>();
    }

    /**
     * Retrieves and removes the last element of this stack. This method 
     * throws an exception if this stack is empty.
     *
     * @return the tail of this deque
     * @throws java.util.NoSuchElementException if this stack is empty
     */
    public V remove() {
        return data.removeLast();
    }

    /**
     * Retrieves, but does not remove, the last inserted element
     * or returns {@code null} if this stack is empty.
     *
     * @return the last inserted element or {@code null} if this stack is empty
     */
    public V top() {
        return data.peekLast();
    }

    public int size() {
        return data.size();
    }

    public void add(V t) {
        data.add(t);
    }

    public void clear() {
        data.clear();
    }

    @Override
    public Iterator<V> iterator() {
        return data.iterator();
    }
}

SRC

答案 1 :(得分:0)

Character(objects) ArrayList不适合保留单个字符。而是使用CharSequence ArrayList,并以与操纵CharSequence相同的方式操纵Strings个对象。