簡體   English   中英

Java在arrayList中的項目的索引

[英]Java index of items in arrayList

我正在開發一個程序,要求用戶提供值,並將其添加到數組列表中,直到插入值-1。 然后它詢問您要搜索什么值,然后用戶插入另一個值。 然后程序在數組列表中搜索所有這些值,並打印出所有找到的項的索引。 由於我正在使用list.indexOf(searcheditem),因此我的程序當前僅打印第一項及其索引。 如何獲取列出所有找到的項目的信息,而不僅僅是第一個? 到目前為止,這是我的代碼。

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

    }
}

提前致謝!

將此for循環放入if條件

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

遍歷列表,比較與searched項目,如果有匹配項,則將索引添加到結果列表中。 大致情況:

List<Integer> foundIndices = new ArrayList<>();
for (int index = 0; index < list.size(); index++) {
    if (list.get(index).intValue() == searched) {
        foundIndices.add(index);
    }
}
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將返回數組列表中的第一個找到的元素。

一個簡單的for循環就足以並且比containsindexOf的組合要快。 您可以將循環索引保留在for語句之外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");
}

如果您想使用.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

}

試試下面的代碼

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());
    for (int i : list) {
        if (i == searched) {
            System.out.println("Value " + searched + " has index of: " + (list.indexOf(searched)));
        }
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM