且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ArrayList中的Java / RMI

更新时间:2023-11-29 16:18:34

由于套装,是itemInfo对象的ArrayList,你可以通过他们循环是这样的:

As ItemSet, is an ArrayList of itemInfo objects, you can loop through them like this:

for(itemInfo info : itemSet){

    System.out.println(info.actionID);
    System.out.println(info.auctionPrice);
    System.out.println(info.buyoutPrice);

}

这将全部打印出来。
也许,当你包括ID,您可以要求用户在ID输入下一个,然后你可以从ArrayList中的那一个。可以通过在它们的所有循环和其ID比较用户输入的ID做到这一点。
例如:

This will print them all out. Perhaps, as you include the ID, you can ask the user to type in the ID next, and then you can retrieve that one from the arraylist. You can do this by looping through them all and comparing their ID to the ID the user entered. For example:

// get the ID
int auctionId = scanner.nextInt();
itemInfo selectedInfo;

// find that item
 for(itemInfo info : itemSet){
    if(info.auctionId = auctionId){
        selectedInfo = info;
        break;
    }
}

if(selectedInfo == null){
    // the ID was not valid!
   // do something to handle this case.
} else {
    System.out.println(selectedInfo.auctionID);
    System.out.println(selectedInfo.auctionPrice);
    System.out.println(selectedInfo.buyoutPrice);
}

当你正在学习,这里有一些东西,使你的code位更好:

1类名称应该以一个大写的开始,你应该改变itemInfo是ItemInfo。

1- Class names should start with an uppercase, you should change itemInfo to be ItemInfo.

2 - 通常应该使用getter和setter方法​​,所以不是用 selectedInfo.auctionID ,你应该使用 selectedInfo.getAuctionId() selectedInfo.setAuctionId(X);

2- You should generally use getters and setters, so instead of using selectedInfo.auctionID, you should use selectedInfo.getAuctionId() and selectedInfo.setAuctionId(x);

3,您或许应该考虑使用交换机而不是如果(scanner.next()。等于(1))。另外,如果你最终是否(scanner.next()。等于(2))writng别人,那么你会遇到一个问题,因为每次scanner.next()被调用,它需要投入,因此期望输入每一个如果。相反,你应该有你的开关外的scanner.next(),然后用它在读取的值,例如:

3- You should probably consider using a switch rather than the if(scanner.next().equals("1")). Also, if you end up writng else if(scanner.next().equals("2")) then you will run into a problem, as each time scanner.next() is called, it expects input, therefore it would expect input for every if. Instead, you should have the scanner.next() outside of your switch, and then use the value which is read in. For example:

int menuSelection = scanner.nextInt();
switch(menuSelection){
    case 1: 
        // do your stuff
        break;
    case 2:
        // do something else
        break;
    default:
        // handle any input which isn't a menu option
 }

4-最后,你应该拆分功能为处理每这些菜单选项中单独的方法。如果你把它全部变成这种方法,会得到非常大的和丑陋的(难以维持)非常快的。

4- Finally, you should probably split the functionality for handling each of these menu options in to separate methods. If you put it all into this method it's going to get very big and ugly (hard to maintain) very fast.