Arraylist - 度假出租屋

时间:2014-04-13 04:07:57

标签: java

当用户输入id和度假村代码时,如何在ArrayList中搜索并获取存储id和度假村代码的数组编号?我正在使用for循环。

for(Person pe: guestList)
{
    if ( pe.getPersonId().equals(Id))   
        System.out.println(pe.toString());
}

for(Resort re: resortList)
{
    if ( re.getResortCode().equals(resortCode)) 
        System.out.println(re.toString());
}

而不是手动输入:

Rental r1 = new Rental(guestList.get(2),resortList.get(3));

以下是我的完整计划。

//create list of resort;
Chalet c1 = new Chalet("001","chalet1",90);
Chalet c2 = new Chalet("002","chalet2",90);
Chalet c3 = new Chalet("003","chalet3",85);
Chalet c4 = new Chalet("004","chalet4",80);
Chalet c5 = new Chalet("005","chalet5",80);
Chalet c6 = new Chalet("006","chalet6",70);

ArrayList <Resort> resortList = new ArrayList<Resort>();
resortList.add(c1);
resortList.add(c2);
resortList.add(c3);
resortList.add(c4);
resortList.add(c5);
resortList.add(c6);


//create list of guest
Person p1 = new Person("001","Alvin","88889999");
Person p2 = new Person("002","Bob","89889999");
Person p3 = new Person("003","Wendy ","98123456");

ArrayList <Person> guestList = new ArrayList<Person>();
guestList.add(p1);
guestList.add(p2);
guestList.add(p3);

Scanner console = new Scanner(System.in);

while(true){

    System.out.print("Enter your id: ");
    String id = console.nextLine();
    for(Person pe: guestList)
    {
        if ( pe.getPersonId().equals(id))
            System.out.println(pe.toString());

    }



    System.out.print("Enter resort code: ");
    String resortCode = console.nextLine();
    for(Resort re: resortList)
    {
        if ( re.getResortCode().equals(resortCode))
            System.out.println(re.toString());

    }

    Rental r1 = new Rental(guestList.get(2),resortList.get(3));
    System.out.println("");
    System.out.println(r1);
    System.out.println("");
    console.nextLine();
}

非常感谢Jason C的解决方案。我完成了我的程序。

    int guestIndex = -1; 

    for(int i = 0; i <guestList.size(); ++ i)
    {
        if ( guestList.get(i).getPersonId().equals(id)) {

          guestIndex = i;
        }
     }


    int resortIndex = -1; 
    for(int n = 0; n < resortList.size(); ++ n)
    {
      if ( resortList.get(n).getResortCode().equals(resortCode)) {
          System.out.println(re.toString());
          resortIndex = n; 
       }
    }

    Rental r1 = new Rental(guestList.get(guestIndex),resortList.get(resortIndex));

3 个答案:

答案 0 :(得分:1)

您似乎已经很好地处理了如何找到您正在寻找的项目。要存储该项目供以后使用,只需在找到它时将其粘贴在变量中即可。

你现在有这个:

for(Resort re: resortList)
{
    if ( re.getResortCode().equals(resortCode))
        System.out.println(re.toString());
}

Rental r1 = new Rental(guestList.get(2),resortList.get(3));

但你可以这样做:

Rental r1 = null; // <= this is where we'll store the found object

for(Resort re: resortList)
{
    if ( re.getResortCode().equals(resortCode)) {
        System.out.println(re.toString());
        r1 = re; // <= keep track of the one we found
    }
}

// now r1 contains the found object, or null if not found

如果你想获得索引本身,对于循环标准来说,这可能是最简单的:

int resortIndex = -1; // <= this is where we'll store the index

for(int n = 0; n < resortList.size(); ++ n)
{
    if ( resortList.get(n).getResortCode().equals(resortCode)) {
        System.out.println(re.toString());
        resortIndex = n; // <= keep track of the index
    }
}

// now resortIndex contains the index into the array, or -1 if not found

您还可以通过其他方式改进代码(例如,break,只要您找到该项目等等),但这一切都超出了此答案的范围。

答案 1 :(得分:0)

我不知道您的确切需求,但您可以使用HashMap将值存储为键值对。这可以减少你满足这一特殊需求的一些步骤

修改 HashMap:哈希映射是一种数据结构,它允许您将数据存储在键值对中,因此您可以使用get方法从密钥中获取值,对于您的案例键,客户ID和价值就是客户。哈希映射很容易实现阅读下面的教程。

教程:http://www.mkyong.com/java/how-to-use-hashmap-tutorial-java/ 文档:http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

答案 2 :(得分:0)

只是添加到Nixit Patel,解决方案......我想它会是这样的:

Map<Integer, Person> guestList = new HashMap<Integer, Person>();
Map<Integer, Chalet> resortList = new HashMap<Integer, Chalet>();

private void execute() {
    loadList();

    Scanner console = new Scanner(System.in);
    while (true) {
        System.out.print("Enter your id: ");
        String id = console.nextLine();

        Person pe = guestList.get(id);
        if (pe != null) {
            System.out.println(pe.getPersonId());
        } else {
            System.err.println("Person not found: " + id);
        }

        System.out.print("Enter resort code: ");
        String resortCode = console.nextLine();
        Chalet re = resortList.get(resortCode);
        if (re != null) {
            System.out.println(re.getId());
        } else {
            System.err.println("Resort Not found: " + id);
        }

        Rental r1 = new Rental(guestList.get(2), resortList.get(3));
        System.out.println("");
        System.out.println(r1);
        System.out.println("");
        console.nextLine();
    }

}

private void loadList() {
    guestList.put(1, new Person("001", "Alvin", "888999888"));
    guestList.put(2, new Person("002", "Bob", "89888999"));
    guestList.put(3, new Person("003", "Wendy", "989123345"));


    resortList.put(1, new Chalet("001", "chalet1", 90));
    resortList.put(2, new Chalet("002", "chalet2", 90));
    resortList.put(3, new Chalet("003", "chalet3", 85));
    resortList.put(4, new Chalet("004", "chalet4", 80));
    resortList.put(5, new Chalet("005", "chalet5", 80));
    resortList.put(6, new Chalet("006", "chalet6", 70));
}