arrayList中项的Java索引

时间:2018-02-21 11:03:16

标签: java arraylist indexof

我正在处理一个向用户询问值的程序,并将它们添加到数组列表中,直到插入值为-1。然后它询问,您要搜索什么值,用户插入另一个值。然后程序在数组列表中搜索所有这些值,并打印出所有找到的项的索引。我的程序目前仅打印第一项及其索引,因为我使用list.indexOf(searchingitem)。如何让它列出所有找到的项目而不仅仅是第一项?到目前为止,这是我的代码。

import java.util.ArrayList;
import java.util.Scanner;

public class KysytynLuvunIndeksi {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        ArrayList<Integer> list = new ArrayList<>();
        while (true) {
            int read = Integer.parseInt(reader.nextLine());
            if (read == -1) {
                break;
            }

            list.add(read);
        }

        System.out.print("What are you looking for? ");
        int searched = Integer.parseInt(reader.nextLine());

        if (list.contains(searched)) {
            System.out.println("Value " + searched + " has index of: " + (list.indexOf(searched));
        } else {
            System.out.println("value " + searched + " is not found");
        }

    }
}

提前致谢!

6 个答案:

答案 0 :(得分:1)

如果条件

,请将此循环放入
if (list.contains(searched)) {
     for (int index = list.indexOf(searched);index >= 0;index =list.indexOf(searched, index + 1))
    {
      System.out.println("Value " + searched + " has index of: " + index);
    }
} 

答案 1 :(得分:0)

遍历列表,将项目与searched进行比较,如果匹配则将索引添加到结果列表中。一些事情:

List<Integer> foundIndices = new ArrayList<>();
for (int index = 0; index < list.size(); index++) {
    if (list.get(index).intValue() == searched) {
        foundIndices.add(index);
    }
}

答案 2 :(得分:0)

public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        ArrayList<Integer> list = new ArrayList<>();
        while (true) {
            int read = Integer.parseInt(reader.nextLine());
            if (read == -1) {
                break;
            }

            list.add(read);
        }

        System.out.print("What are you looking for? ");
        int searched = Integer.parseInt(reader.nextLine());

        for(int i=0;i<list.size();i++){
            if(list.get(i)==searched){
                System.out.println("Value " + searched + " has index of: " + (i));
            }
        }
        if (!list.contains(searched)){
            System.out.println("value " + searched + " is not found");
       }    
    }

简单的for循环就足够了。

list.contains将返回数组列表中第一个找到的元素。

答案 3 :(得分:0)

简单的for循环就足够了,并且比containsindexOf的组合更快。您可以将循环索引保留在for语句之外,以检查之后是否找不到任何内容。

int i;
for (i = 0; i < list.size(); i++) {
    if (searched == list.get(i)) {
        System.out.println("Value " + searched + " has index of: " + i);
    }
}
if (i == list.size()) {
    System.out.println("value " + searched + " is not found");
}

答案 4 :(得分:0)

如果你想使用.contains()方法,我会建议这样做:

ArrayList<Integer> bufflist = list.clone(); // you create a temporal back of the list to operate with the same list without destroying the data
int searched = Integer.parseInt(reader.nextLine());

while (bufflist.contains(searched)) {
    System.out.println("Value " + searched + " has index of: " + (list.indexOf(searched));
     listbuff.remove(list.indexOf(searched)); //you remove from the list so it doesn´t find it again

}

答案 5 :(得分:-1)

尝试以下代码

GlobalKey

}