Java 如何在不使用堆栈的字符数组列表中将数字作为一个读取
我正在创建一个字符数组列表来读取我输入的每一个字符。如果我的输入为0-9,我的ctr将为+1,当我的输入为运算符时,我的ctr将为-1,但我如何才能将该数字(例如12)作为一条语句来读取?我能得到的最好的选择是什么 这是我的密码:Java 如何在不使用堆栈的字符数组列表中将数字作为一个读取,java,arraylist,Java,Arraylist,我正在创建一个字符数组列表来读取我输入的每一个字符。如果我的输入为0-9,我的ctr将为+1,当我的输入为运算符时,我的ctr将为-1,但我如何才能将该数字(例如12)作为一条语句来读取?我能得到的最好的选择是什么 这是我的密码: 类验证2 { 静态扫描仪输入=新扫描仪(System.in); 静态字符串确认=null; 公共静态void main(字符串参数[]) { int ctr=0; ArrayList=新建ArrayList(); 字符串表示法; 做 { System.out.prin
类验证2
{
静态扫描仪输入=新扫描仪(System.in);
静态字符串确认=null;
公共静态void main(字符串参数[])
{
int ctr=0;
ArrayList=新建ArrayList();
字符串表示法;
做
{
System.out.println(“输入后缀符号:”);
notation=input.nextLine();
if(notation.matches(“[A-Za-z]”)和notation.matches(“[\\p{Punct}&[^]]+”)
{
System.out.println(“无效输入抱歉”);
}
else if(符号字符(0)=9)
{
对于(int x=0;x
A字符(对象)ArrayList
不适合保存单个字符。相反,请使用CharSequence ArrayList
,并像处理字符串一样操作CharSequence
对象。不要使用数组列表作为后缀符号。使用简单堆栈并根据运算符删除数字。堆栈可以可以用一个简单的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();
}
}
包tk.manf.util.collection;
导入java.util.Deque;
导入java.util.Iterator;
导入java.util.LinkedList;
/**
*使用LinkedList的后进先出实现
*@作者比约恩·海因里希斯
*/
公共类堆栈实现了Iterable{
私人最终数据;
公共堆栈(){
this.data=新的LinkedList();
}
/**
*检索并删除此堆栈的最后一个元素。此方法
*如果此堆栈为空,则引发异常。
*
*@把这条龙的尾巴还给我
*如果此堆栈为空,则@throws java.util.NoSuchElementException
*/
公共V删除(){
返回data.removeLast();
}
/**
*检索但不删除最后插入的元素
*如果此堆栈为空,则返回{@code null}。
*
*@返回最后插入的元素,如果此堆栈为空,则返回{@code null}
*/
公共V top(){
返回data.peekLast();
}
公共整数大小(){
返回data.size();
}
公共空间添加(V t){
数据。添加(t);
}
公共空间清除(){
data.clear();
}
@凌驾
公共迭代器迭代器(){
返回data.iterator();
}
}
()好吧,你说你有一个字符列表,12
是两个字符,一个1
和一个2
。你是如何得到字符数组的?它最初是一个字符串,你已经将它转换成了一个字符数组,你想让数字保持组合为一个数字吗?你能在循环之前再发布一些代码吗,比如列表的位置和输入的示例。使用字符数组和开关。字符也不是数字。if语句有缺陷。(0-9是字符的唯一有效数字)。为什么不在String类中使用subString方法,然后输入它进行比较呢。
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();
}
}