分裂使用一个ArrayList的方程式 [英] Splitting up an equation using an ArrayList

查看:185
本文介绍了分裂使用一个ArrayList的方程式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我输入的公式如下:

  9 + 3 * 2/1

我的输出是:

  [9,3,2,1]
[+,*,/]

为什么我的第二个数组开始了,和我怎么摆脱它,因此输出将是

  [+,*,/]

字符串评估(字符串EXP){        字符串SETEX pression = expr.getText();        的String []号= SETEX pression.split([* / + - ]);
        的String [] = OPS SETEX pression.split([123456789]);        ArrayList的<串GT; setNumbers =新的ArrayList<串GT;();
        ArrayList的<串GT; setOps =新的ArrayList<串GT;();
        的for(int i = 0; I< numbers.length;我++){
            setNumbers.add(数字[I]);
        }        的for(int i = 0; I< numbers.length;我++){
            setOps.add(OPS [I]);
        }        的System.out.println(setNumbers);
        的System.out.println(setOps);        返回EXP;
    }
}


解决方案

你类似的一个例子:

 公共类DemoSplit {
    公共静态无效的主要(字串[] args){
        字符串s =1,2;
        串ス[] = s.split(,);
        对于(字符串ST:SU)的System.out.println('+ ST +');
    }
}

将会打印:

 ''
'1'
'2'

第一个字符是一个分隔符,所以出现分裂和元素之前,它是个esplitted数组的第一个元素。

在你的情况,你spliut的数字:

  9 + 3 * 2/1

9日之前,空元素后,'+'...

如果你觉得像在看code,<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.split%28java.lang.String%29\"相对=nofollow> String.split 调用<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/6-b14/java/util/regex/Pattern.java#Pattern.split%28java.lang.CharSequence,int%29\"相对=nofollow> java.util.regex.Pattern.split

在拆分方法,你会发现增加了一个ArrayList索引和匹配的子字符串,因为指数是第一次迭代为零,本例中的第一场比赛是在零,第一个元素是一个空字符串。从分裂法的来源$ C ​​$ C:

 的ArrayList&LT;串GT; matchList =新的ArrayList&LT;串GT;();
    匹配器M =匹配(输入);    //发现每场比赛之前添加段
    而(m.find()){
        如果(matchLimited || matchList.size()&LT;!极限 - 1){
            字符串匹配= input.subSequence(索引,m.start())的toString()。
            matchList.add(匹配);
            指数= m.end();
        }否则如果(matchList.size()==限 - 1){//最后一个
            字符串匹配= input.subSequence(指数,
                                             。input.length())的toString();
            matchList.add(匹配);
            指数= m.end();
        }
    }

If I enter an equation like:

9+3*2/1

My output is:

[9,3,2,1] 
[,+,*,/]

Why does my second array start off with a "," and how do I get rid of it so the output would be

[+,*,/]

String evaluate(String exp) {

        String setExpression = expr.getText();

        String[] numbers = setExpression.split("[*/+-]");
        String[] ops = setExpression.split("[123456789]");

        ArrayList <String> setNumbers = new ArrayList <String>();
        ArrayList <String> setOps = new ArrayList <String>();


        for(int i=0; i<numbers.length; i++){
            setNumbers.add(numbers[i]);
        }

        for(int i=0; i<numbers.length; i++){
            setOps.add(ops[i]);
        }

        System.out.println(setNumbers);
        System.out.println(setOps);

        return exp;
    }
}

解决方案

An example similar to yours:

public class DemoSplit  {   
    public static void main(String[] args) {
        String s = ",1,2,";
        String su[] = s.split(",");
        for (String st: su) System.out.println("'"+st+"'");
    }
}

Will print:

''
'1'
'2'

The first char is a delimiter, so split there and the element before it is the first element of th esplitted array.

In your case you spliut on numbers:

9+3*2/1

before 9, empty element, after '+' ...

If you feel like looking at the code, String.split calls java.util.regex.Pattern.split

In the split method, you will see that adds to an ArrayList the subStrings between index and the match, since index is zero for the first iteration and the first match in the example is at zero, the first element is an empty string. From the source code of the split method:

    ArrayList<String> matchList = new ArrayList<String>();
    Matcher m = matcher(input);

    // Add segments before each match found
    while(m.find()) {
        if (!matchLimited || matchList.size() < limit - 1) {
            String match = input.subSequence(index, m.start()).toString();
            matchList.add(match);
            index = m.end();
        } else if (matchList.size() == limit - 1) { // last one
            String match = input.subSequence(index,
                                             input.length()).toString();
            matchList.add(match);
            index = m.end();
        }
    }

这篇关于分裂使用一个ArrayList的方程式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆