Java 基于扫描程序输入是否与同一索引内的单词匹配返回ArrayList索引的内容

Java 基于扫描程序输入是否与同一索引内的单词匹配返回ArrayList索引的内容,java,arraylist,java.util.scanner,Java,Arraylist,Java.util.scanner,我想返回ArrayList索引的内容,基于扫描仪输入是否匹配所述索引中的任何单词 所以如果ArrayList索引0包含 <dog, cat, mouse>` <football, baseball, rugby> 下面的代码读取输入,如果在editList中找到editScanner输入,则应打印索引。但是,我知道这是不正确的,但我完全不确定如何将索引的内容打印为字符串 if (editList.contains(editScanner)) {

我想返回ArrayList索引的内容,基于扫描仪输入是否匹配所述索引中的任何单词

所以如果ArrayList索引0包含

<dog, cat, mouse>`
<football, baseball, rugby>
下面的代码读取输入,如果在editList中找到editScanner输入,则应打印索引。但是,我知道这是不正确的,但我完全不确定如何将索引的内容打印为字符串

 if (editList.contains(editScanner)) {
                        while (((edit = fileReader.readLine()) != null)) {
                            String chosenFilm = editList.indexOf(editScanner);
                        }

通过以下方式将列表设置为集合的阵列列表:

 List<Set<String>> yourList = new ArrayList<>();

如果无法在各种索引上重复足球,请使用地图,使用足球作为键,索引football is in作为值。然后,您可以检查索引并直接从列表中打印该索引。

考虑到您的结构是一个
列表
输入

List<List<String>> list = new ArrayList<>();
list.add(Arrays.asList("dog", "cat", "mouse"));
list.add(Arrays.asList("football", "baseball", "rugby"));

Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
  • 使用

    index = list.stream()                       // iterate
                .filter(l -> l.contains(input)) // keep sublist where present
                .mapToInt(list::indexOf)        // transform it to its index
                .findFirst()                    // keep index
                .orElse(-1);                    // if not found return -1
    

  • 您的问题只包含需求-它并没有显示您自己为解决此问题所做的任何努力。请添加您对此问题的尝试-因为此网站不是免费的“我们为您(家庭)工作”服务。除此之外:请转到第页了解如何/问什么。谢谢你最好发一些代码,看看你尝试了什么你已经试过写这个了吗?您知道如何将这些值存储在
    列表中吗?你知道如何从
    扫描仪中读取数据吗?你能解释一下你所说的“如何做到这一点,这样任何建议都会很棒”是什么意思吗?在java中,我们不必关心“设备”,JVM在任何地方都“相同”运行。这是一个很大的问题,但你现在连一个都没有。我的定义是什么?如果我是对的,那肯定是索引?是的,我会是索引。顺便说一句,这是一个比其他答案(使用子列表)更快、更符合逻辑的实现。显然,每个索引中都有一组字符串,而不是子列表。原因集更快是在哈希集中使用字典访问时间使用哈希代码(恒定时间)而不是逐个字符串检查所有子列表索引(n次)。
    
    List<List<String>> list = new ArrayList<>();
    list.add(Arrays.asList("dog", "cat", "mouse"));
    list.add(Arrays.asList("football", "baseball", "rugby"));
    
    Scanner sc = new Scanner(System.in);
    String input = sc.nextLine();
    
    int index = -1;
    for (int i = 0; i < list.size(); i++) {     // iterate
        if (list.get(i).contains(input)) {      // check if present
            index = i;                          // keep index
            break;
        }
    }
    
    index = list.stream()                       // iterate
                .filter(l -> l.contains(input)) // keep sublist where present
                .mapToInt(list::indexOf)        // transform it to its index
                .findFirst()                    // keep index
                .orElse(-1);                    // if not found return -1