【问题标题】:How to find the nth Index in a arrayList如何在arrayList中找到第n个索引
【发布时间】:2017-05-06 20:05:13
【问题描述】:

如何在数组列表中找到第 n 个索引 例如:我是List<Character> charList = new ArrayList<Character>(Arrays.asList('s', 'h','a','r','a','n'))

charList.indexOf('a'); 

总是给出afirst 索引。 我如何获得第 n 个?

【问题讨论】:

  • 使用lastIndexOf('a') ..
  • 听起来像XY Problem。为什么要将字符存储在 List 而不是 String 中?为什么需要访问 char 的第 n 个索引?
  • 感谢您让我知道 XY 问题,听起来很有趣。是的,当然我没有给出上下文抱歉,问题是找到任何元素的第 n 个索引,在我的情况下,它恰好是字符 ..在 List字符串列表

标签: java list arraylist collections


【解决方案1】:

ArrayList中indexOf()的实现:

    public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

所以你可以尝试列出这个:

    public int indexNth(List charList, int n, Object _enum) {
        int index = 0;
        int findTimes = 0;
        if (n == 0)
            return -1;
        if (CollectionUtils.isEmpty(charList))
            return -1;
        for (Object o : charList) {
            if (o.equals(_enum))
                findTimes++;
            if (findTimes >= n)
                return index;
            index++;
        }
        return -1;
    }

【讨论】:

    【解决方案2】:

    我会为它创建一个方法,如下所示:

    public static <T> int indexOfNth(List<T> list, T find, int nthOccurrence) {
        if (list == null || list.isEmpty()) return -1;
        int hitCount = 0;
        for (int index = 0; index < list.size(); index++) {
            if (list.get(index).equals(find)) {
                hitCount++;
            }
            if (hitCount == nthOccurrence) return index;
        }
        return -1;
    }
    

    【讨论】:

      【解决方案3】:

      这是一个基于 Java 8 Streams 的方法:

          List<Character> charList = new ArrayList<>(Arrays.asList('s', 'h','a','r','a','n'));
      
          int[] allIndexes = IntStream.range(0, charList.size())
                                      .filter(i -> charList.get(i).equals('a'))
                                      .toArray();
      
          System.out.println(Arrays.toString(allIndexes));
      

      这会在输入数组charList 中搜索与所需元素'a' 相等的所有元素,并返回一个包含匹配元素索引的数组。如果 N 小于 allIndexes.length,则第 N 次出现在 allIndexes[N],否则没有第 N 次出现。 (这假设 N 是从零开始的,就像 Java 中的数组和列表索引一样。)

      在这种情况下,结果是

          [2, 4]
      

      【讨论】:

        【解决方案4】:

        indexOf('a') 返回列表中第一个“a”元素的索引。

        为了找到第n个'a'的索引,你需要遍历列表:

        int count = 0;
        int index = -1;
        for (int i = 0; i < charList.size(); i++) {
            if charList[i].equals('a') {
                count++;
            }
            if (count == 2) {
                index = i;
                break;
            }
        }
        

        我假设您正在寻找第二个“a”。

        【讨论】:

        • 也许他想得到第二个a的索引。现在你会说使用 lastindexof。但是如果有3个a呢?如何获得第二高效率?
        • 有无数的原因可以让我们找到x 在数组中的位置。由于 Java 的 .contains(Object o).indexOf(Object o) 都使用了 Object.equals(Object o)(正如他们应该的那样),因此可以非常容易地根据该对象的某些属性在数组中找到用户定义的对象.我无法告诉你我使用这个功能的次数。这绝不是无用的……在多维数组和并行数组的情况下尤其如此。
        • 对不起,我编辑了答案。感谢您的 cmets。
        【解决方案5】:

        下面的代码 sn-p 将从 arraylist 返回所需字符的位置。

        import java.util.ArrayList;
        import java.util.Arrays;
        import java.util.Iterator;
        
        
        class Test4
        {
        
            public static int find(ArrayList<Character> obj,int index,char value)
            {
        
                int counter=0;
                for(int i=0;i<obj.size();i++)
                {
        
                    if(obj.get(i)==value)
                    {
                        counter++;
                        if(counter==index)
                        {
                            return i+1;
                        }
                    }
                }
                    return -1;
        
            }
        
             public static void main(String[] args) {
        
               ArrayList<Character> charList = new ArrayList<Character>(Arrays.asList('s', 'h','a','r','a','n'));
        
        
          // The following parameters to method find are as find the position of second character 'a' in the ArrayList
        
               System.out.println(find(charList,2,'a'));
        
            }
        
        }
        

        如果字符不存在,则返回-1。

        【讨论】:

          【解决方案6】:
          public static int indexOf(List<Object> tokens, int n, Object obj) {
                  int times = 0;
                  for (int i = 0; i < tokens.size(); i++) {
                      if (tokens.get(i) == obj && n == ++times) {
                          return ++i;
                      }
                  }
                  return -1;
              }
          

          【讨论】:

            猜你喜欢
            • 2014-04-11
            • 1970-01-01
            • 2012-01-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多