【问题标题】:Java index of items in arrayListarrayList 中项目的 Java 索引
【发布时间】:2018-02-21 11:03:15
【问题描述】:

我正在开发一个程序,该程序向用户询问值,并将它们添加到数组列表中,直到插入值 -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");
        }

    }
}

提前致谢!

【问题讨论】:

  • 您好,欢迎来到 StackOverflow!为什么不使用与填写列表相同的方式?但是第二个列表女巫将包含搜索的值?
  • 看到这个question
  • 在您的问题中,我碰巧注意到,您提到“程序在数组列表中搜索所有这些值”,您是否推断您的程序必须采用超过 1 个值进行搜索?因为您的程序确实只需要 1 个输入。

标签: java arraylist indexof


【解决方案1】:

把这个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);
    }
} 

【讨论】:

    【解决方案2】:

    遍历列表,将项目与searched 进行比较,如果匹配,则将索引添加到结果列表中。类似的东西:

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

    【讨论】:

    • 考虑使用ListIterator 迭代输入值。
    • @Henrik 为什么? (诚​​实的问题。)
    • 尝试了快速重写,但可读性根本没有提高,这通常是人们在迭代列表并需要索引时的情况。很少有人知道ListIterator。 :)
    • @Henrik 我非常感谢你的努力和认真。
    • 如果列表是非随机访问类型,例如LinkedListListIterator 可以(并且应该)更快。通过索引访问链表的所有元素需要 O(n**2) 时间; ListIterator 在 O(n) 中完成。对于ArrayList,在这种情况下,无论哪种方式都是O(n),所以如果不使用ListIterator,不会有很大的损失。
    【解决方案3】:
    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 将返回数组列表中找到的第一个元素。

    【讨论】:

      【解决方案4】:

      一个简单的 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");
      }
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        • 这里有几个问题。您实际上并没有使用bufflist 创建新列表,而只是引用和更改原始列表。此外,在循环内,您将删除元素,然后使用 indexOf 方法搜索它。可能应该在循环内切换两行。
        • @Henrik 已修复!
        【解决方案6】:

        试试下面的代码

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

        }

        【讨论】:

        • 不,它只会打印第一次出现的索引,无论该数字在列表中出现多少次。
        • 对,这基本上是在重复题主的问题。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-22
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-16
        相关资源
        最近更新 更多