【问题标题】:how to find all occurrence with indexes of string in an ArrayList javajava - 如何在ArrayList java中查找所有出现的字符串索引
【发布时间】:2018-08-08 10:53:03
【问题描述】:

拥有一个包含如下数据的 ArrayList。我的要求是找到所有数据长度为4的索引。

values.add("1000");
values.add("10001111");
values.add("45678901");
values.add("1111");
values.add("22222222");
values.add("2222");
values.add("33333333");

【问题讨论】:

  • 试图实现这一目标的代码在哪里?
  • 如果您付出最少的努力或告诉我们出了什么问题,我们可以帮助您找到解决方案。这似乎是家庭作业,我们不会为您做。
  • 阅读以下帮助主题:stackoverflow.com/help/how-to-ask

标签: java arraylist collections


【解决方案1】:

你需要:

  • 遍历值
  • 检查长度
  • 如果条件通过则保留索引

  1. Workable demo : 使用Streams 可以内联解决方案以获得Listint[]

    List<Integer> indexes = values.stream().filter(s -> s.length() == 4)
                                           .map(values::indexOf)
                                           .collect(Collectors.toList());
    
    int[] indexesArray = values.stream().filter(s -> s.length() == 4)
                                        .mapToInt(values::indexOf)
                                        .toArray();
    
  2. Workable demo : 使用经典的for loop

    List<Integer> indexes = new ArrayList<>();
    for(int i=0; i<values.size(); i++){
        if(values.get(i).length() == 4){
            indexes.add(i);
        }
    }
    

【讨论】:

    【解决方案2】:

    您可以创建一个IntStream 的索引:

    IntStream allIndices = IntStream.range(0, values.size());
    

    然后您可以根据您提供的条件进行过滤:

    IntStream filteredIndices = allIndices.filter(i -> values.get(i).length() == 4);
    

    最后,您可以将这些索引转换为您喜欢的任何数据结构。

    一个数组:

    int[] indices = filteredIndices.toArray();
    

    或列表

    List<Integer> indices = filteredIndices.boxed().collect(Collectors.toList());
    

    作为一个声明:

    int[] indices = IntStream.range(0, values.size())
        .filter(i -> values.get(i).length() == 4)
        .toArray();
    

    【讨论】:

    • 这个是快速排序的。谢谢@Lino。
    【解决方案3】:

    当您迭代 values 并随后输出该 Map 的内容时,您可以将匹配值及其索引/索引存储在 Map&lt;Integer, String&gt; 中:

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class Test {
    
        public static void main(String[] args) {
            List<String> values = new ArrayList<String>();
    
            values.add("1000");
            values.add("10001111");
            values.add("45678901");
            values.add("1111");
            values.add("22222222");
            values.add("2222");
            values.add("33333333");
    
            // create a storage structure for index and value
            Map<Integer, String> valuesWithLengthFour = new HashMap<Integer, String>();
    
            // iterate over your list in order to get all the matching values and indexes
            for (int i = 0; i < values.size(); i++) {
                String value = values.get(i);
                if (value.length() == 4) {
                    valuesWithLengthFour.put(i, value);
                }
            }
    
            System.out.println("The following matching values were found in \"values\":");
    
            // print out the content of the storage structure
            for (int key : valuesWithLengthFour.keySet()) {
                System.out.println("Index: " + key + ", Value: " + valuesWithLengthFour.get(key));
            }
        }
    }
    

    有不同的方法可以实现这一点...对于一个班轮,请尝试您问题下方的其中一个 cmets 中所述的代码@Lino。

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 2012-11-15
      • 2019-12-10
      • 2022-01-20
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 2018-06-13
      • 2013-02-05
      相关资源
      最近更新 更多