比较和检索的ArrayList元素 [英] Compare and retrieve elements from ArrayList

查看:146
本文介绍了比较和检索的ArrayList元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个简单的字典,一个字符串比较的的ArrayList A字,然后返回从列表中选择不同的值。在的ArrayList 与外国字的布局,再其次是英语相当于这样的想法是,我在一个字类型,用扫描仪将其比作数组列表那么所以如果我键入单词在列表7日,我希望它回归了8个字,并把它打印出来返回的索引值+1。

I'm trying to build a simple dictionary that compares a string to a word on the ArrayList and then returns a different value from the list. The ArrayList is laid out with the foreign word and then followed by the English equivalent so the idea is that I type in a word, use scanner to compare it to the array list and then return the index value +1 so if the word I type is 7th on the list, I want it to return the 8th word and print it out.

我有一个输入字符串和比较它的基本的想法,但我不知道如何从的ArrayList 返回下面的字:

I've got the basic idea of inputting a string and comparing it, but I don't know how to return the following word from the ArrayList:

public void translateWords(){
        String nameSearch;
        nameSearch=input.nextLine();

        for (Phrase c:phrases) {
            if (c.getName().equals(nameSearch))  {
                System.out.println( c.advancedToString());      
                return;
            }
        }
        System.out.println("not on list");

我试着打约与ArrayList的get方法,但我对如何使用它不确定,因此任何反馈会在这里pciated非常AP $ P $。

I've tried playing about with the get method for the ArrayList but I'm unsure on how to use it so any feedback would be very appreciated here.

推荐答案

的for-each 是不是在这种情况下,适当的,因为你不能访问连续的元素,你的情况翻译的单词。
因此,我建议你使用迭代

for-each is not appropriate in this case because you can not access successive elements, in your case translated words. So I suggest you to use Iterate

我想短语的类型为列表与LT;这句>

public void translateWords(){
        String nameSearch;
        nameSearch=input.nextLine();

        Iterator<Phrase> it = phrases.iterator();
        while(it.hasNext())
        {
           Phrase c = it.next();
           if (c.getName().equals(nameSearch))  {
                System.out.println( it.next().advancedToString());      
                return;
            }
        }

        System.out.println("not on list");
}

这篇关于比较和检索的ArrayList元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆