从字符串数组中过滤数字并获取android中最后两个更大数字的索引

时间:2018-08-07 08:14:20

标签: android arraylist

我有一个ArrayList,其值类似于[Value,Sum3,121,data8input,in:21 :: 7,7.00,9.01],我只想提取数字,因为输出应该像这样[3,121,8, 21,7,7.00,9.01],然后必须重新排列升序,然后获取最后两个数字的索引,结果将是[21,121]。

我下面的尝试代码,

 for (int i = 0; i < arrayString.size(); i++) {
                    Pattern p = Pattern.compile("-?\\d+(,\\d+)*?\\.?\\d+?");
                    List<String> numbers = new ArrayList<String>();
                    Matcher m = p.matcher(arrayString.get(i).getvalue);
                   numbers.addAll(m);
                    for (int j = 0; j < numbers.size(); j++) {
                            Log.d("REMEMBERFILTER", allCollection.get(i).getTextValue());
                        }
                    }
                }

2 个答案:

答案 0 :(得分:1)

做这样的事情,尽管它不能像我使用其他列表那样精确地提高内存效率。

ArrayList<String> tempList = new ArrayList<>();
            for (int i = 0; i < yourArrayList.size(); i++) {
                tempList.add(yourArrayList.get(i).replaceAll("[^0-9]", ""));
            }

    //Arrange in ascending order
  Collections.sort(tempList);
//Also try to remove those indexes which has only letters with
    tempList.removeAll(Arrays.asList("", null));
            for (int i = 0; i < tempList.size(); i++) {
                Log.d("+++++++++", "" + tempList.get(i));
            }
//You can get the last two or any element by get method of list by //list.size()-1 and list.size()-2 so on 

答案 1 :(得分:0)

这是一种实现方式,\begin{table*} \begin{tabular}{r|r} \hline speed & dist\\ \hline 4 & 2\\ \hline \end{tabular} \end{table*} 具有您想要的2个数字:

finalArray