Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在arrayList中查找第n个索引_Java_List_Arraylist_Collections - Fatal编程技术网

Java 如何在arrayList中查找第n个索引

Java 如何在arrayList中查找第n个索引,java,list,arraylist,collections,Java,List,Arraylist,Collections,如何在arraylist中查找第n个索引 例如:i hv aList charList=newarraylist(Arrays.asList('s','h','a','r','a','n')) 始终给出a的第一个索引。 如何获取第n个?索引('a')返回列表中第一个'a'元素的索引 为了找到第n个'a'的索引,您需要遍历列表: int count = 0; int index = -1; for (int i = 0; i < charList.size(); i++) { if

如何在arraylist中查找第n个索引 例如:i hv a
List charList=newarraylist(Arrays.asList('s','h','a','r','a','n'))

始终给出
a
的第一个索引。 如何获取第n个?

索引('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;
    }
}
int count=0;
int指数=-1;
对于(int i=0;i
我假设您正在查找第二个“a”。

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;
    }
}
int count=0;
int指数=-1;
对于(int i=0;i
我假设您正在寻找第二个“a”。

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;
    }
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 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;
}
public static int indexOfNth(List List,T find,int nthOccurrence){
if(list==null | | list.isEmpty())返回-1;
整数命中率=0;
对于(int index=0;index
我会为它创建一个方法,如下所示:

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;
}
public static int indexOfNth(List List,T find,int nthOccurrence){
if(list==null | | list.isEmpty())返回-1;
整数命中率=0;
对于(int index=0;index
以下代码段将从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'));

    }

}
import java.util.ArrayList;
导入java.util.array;
导入java.util.Iterator;
类Test4
{
公共静态整型查找(ArrayList对象、整型索引、字符值)
{
int计数器=0;

对于(int i=0;i,以下代码段将从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'));

    }

}
import java.util.ArrayList;
导入java.util.array;
导入java.util.Iterator;
类Test4
{
公共静态整型查找(ArrayList对象、整型索引、字符值)
{
int计数器=0;

对于(int i=0;i,这里有一个基于Java 8流的方法:

    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));

以下是一种基于Java 8流的方法:

    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));

使用
lastIndexOf('a'))
。听起来像是。为什么要将字符存储在
列表中而不是
字符串中呢?为什么需要访问字符的第n个索引?感谢您让我知道XY问题,听起来很有趣。是的,我当然没有给出上下文抱歉,问题是要找到任何元素的第n个索引,在我的情况下,它是ppens是字符..如果是List StringList,如何查找给定字符串的第n个索引使用
lastIndexOf('a'))
。听起来像是。为什么要将字符存储在
列表中而不是
字符串中呢?为什么需要访问字符的第n个索引?感谢您让我知道XY问题,听起来很有趣。是的,我当然没有给出上下文抱歉,问题是要找到任何元素的第n个索引,在我的情况下,它是可能他想得到第二个a的索引。现在你会说使用lastindexof。但是如果有3个a呢?你如何得到第二个最有效的?有无数的理由为什么人们想找到
x
在数组中的位置。Beca使用Java的
.contains(Object o)
.indexOf(Object o)
都使用
对象的
.equals(Object o)
(它们应该这样做),可以很容易地根据某个对象的某些属性在数组中找到用户定义的对象。我无法告诉您我使用此功能的次数。这绝对不是无用的…多维数组和并行数组尤其如此。很抱歉,我编辑了答案。谢谢也许他想得到第二个a的索引。现在你会说使用lastindexof。但是如果有3个a呢?你如何得到第二个最有效的?有无数的理由为什么人们想找到
x
在数组中的位置。因为Java的
包含(对象o)
.indexOf(对象o)
都使用
对象的
.equals(对象o)
(它们应该这样做),可以很容易地根据某个对象的某些属性在数组中找到用户定义的对象。我无法告诉您我使用此功能的次数。这绝对不是无用的…多维数组和并行数组尤其如此。很抱歉,我编辑了答案。谢谢谢谢你的评论。