从 arraylist 特定变量中获取随机对象 [英] Get random object from arraylist specific variable

查看:37
本文介绍了从 arraylist 特定变量中获取随机对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数组列表中获取一个随机对象.random 应该在 arraylist 中变量可用为 true 的对象中.

I'm trying to get a random object from an arraylist. The random should be among the object in the arraylist having the variable available as true.

现在它从整个数组列表中随机获取一个服务员.我只需要它来指定服务员为真(变量)的限制这是行:

Right now it get a random attendant from the whole arraylist. I just need it to specify a limit to attendants being true(variable) this is the line:

return myAtt.get(randAtt.nextInt(myAtt.size()));

这是方法:

public static Attendant  askForAtt() {
        Scanner scanAtt = new Scanner(System.in);
        Random randAtt = new Random();
        //Attendant  asnAtt = null;
        System.out.println("Do you require an Attendant ? Y or N");
        String response = scanAtt.next();
        if ((response.equals("y")) || (response.equals("yes")) || (response.equals("Yes")) || (response.equals("Y"))) {
            // Cars.setAssignedTo(myAtt.get(randAtt.nextInt(myAtt.size())));
            return myAtt.get(randAtt.nextInt(myAtt.size()));

        } else if ((response.equals("n")) || (response.equals("no")) || (response.equals("No")) || (response.equals("N"))) {
            return new Attendant ("User");
        }
        return new Attendant ("User");    //If input is neither Yes nor No then return new Attendant    
    }

我应该输入什么?我的服务员是这样的:

What am I supposed to type? My attendants are like that:

public Attendant(int staffNum, String id, boolean available, attNm name, Cars assign) {
        this.staffNum = staffNum;
        this.id = id;
        this.available = available;
        this.name = name;
        this.assign = assign;
    }

PS:对不起我的英语.这是我的第三种语言

PS:Sorry for my English. It's my 3rd language

推荐答案

在你的Attendant类上,添加这个功能

On your Attendant class, add this function

public boolean isAvailable(){
   return available;
}

然后

public static Attendant  askForAtt() {

    ...

    if ((response.equals("y")) || (response.equals("yes")) || (response.equals("Yes")) || (response.equals("Y")) && someoneIsAvailable()) {

        ArrayList<Attendant> temp = getAvailableAttendants();
        Attendant attendant = temp.get(randAtt.nextInt(temp.size()));
        return attendant;

    } 

   ...   
}

public boolean someoneIsAvailable(){
    for(int i = 0; i<myAtt.size(); i++){
        if(myAtt.get(i).isAvailable()){
           return true;
        }
    }
    return false;
}

public ArrayList<Attendant> getAvailableAttendants(){
    ArrayList<Attendant> availableAtt = new ArrayList<>();
    for(int i = 0; i<myAtt.size(); i++){
        Attendant att = myAtt.get(i)
        if(att.isAvailable()){
           availableAtt.add(att);
        }
    }
    return availableAtt;
}

另外,你可以使用

String.equalsIgnoreCase(String);

因为在你的陷阱中,用户可能会做yEs".希望有帮助.如果有什么问题请告诉我

cause in your trapping the user could do "yEs". Hope that helps. Tell me if something is wrong

这篇关于从 arraylist 特定变量中获取随机对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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