使用ArrayList条目进行不安全的转换

时间:2012-07-25 17:14:25

标签: java generics arraylist

当我选择选项'a'时,我可以在我的所有ArrayList中存储一些值。 但是当我选择'b'并尝试检索我输入的内容并存储在变量rq中(以检查预订的房间数)并在选项'b'中使用它循环来循环它。但是所有数组都是空的。

编译时我也遇到“错误”。

在选项'b'for循环中,我做了这个

for(int i=0;i<all.get(4);i++)

但我收到错误:error: bad operand types for binary operator '<'

所以我决定施展它。

for(int i=0;i<(Integer)all.get(4);i++)

现在没有错误,但编译似乎有问题。

  

注意:C:\ Users \ MegaStore \ Desktop \ java testing \ RoomSelection.java使用未经检查或不安全的操作。   注意:使用-Xlint重新编译:取消选中以获取详细信息。

所以我想知道我输入的那些值是在堆中,还是当我选择选项'b'时被垃圾收集器删除。

我的代码尚未完全输入,但我想解决手头的问题。

我的程序还需要记录用户想要和存储的加载项数量(最多4个)。所以我不太确定在哪里添加和存储它。

import java.util.*;
import java.io.*;

public class test123{

public test123()throws InputMismatchException {

    String choiceStr;//initialize choiceStr which is use for reading lines from scanner input
    char choiceChar;//initialize choiceStr which is use for reading character from scanner input
    int choice,rt,rq,ao1,night,ao2,quan;//initialize choiceStr which is use for reading integer from scanner input
    String datee;
    String[] roomType = {"Single Room", "Double Room", "Deluxe Room", "Junior Room", "Suite"}; //Initialize a array for room type
    Integer[] priceRoom = {160,200,280,380,500}; //Initialize a array for room prices
    Integer[] priceAdd = {25,60,70,100}; //Initialize a array for add-on prices
    ArrayList<String> roomAndDate = new ArrayList<String>();
    ArrayList<Integer> all = new ArrayList<Integer>();
    Scanner input= new Scanner(System.in); //Initialize a scanner input

    System.out.println("Room Selection");
    System.out.println("==============\n");
    System.out.println("[a] Room Type");
    System.out.println("[b] Add-Ons");
    System.out.println("[c] Main Menu");
    System.out.println("Type 'a' to select Room Type and state the desire quantity for each type.");
    System.out.println("Type 'b' to select the Add-Ons.");
    System.out.println("Type 'c' to exit from  the Booking Menu.");
    System.out.println("Please enter your option (a, b or c): ");

    ;
    choiceStr = input.nextLine();
    choiceChar = choiceStr.charAt(0); 

    switch(choiceChar){
        case 'a':   System.out.println("Room Type");
                    System.out.println("=====================================================");
                    System.out.println("(1) Single Room (1 person) - Price: S$160");
                    System.out.println("(2) Double Room (2 persons) - Price: S$200");
                    System.out.println("(3) Deluxe Room (2 persons) - Price: S$280");
                    System.out.println("(4) Junior Suite (2 persons) - Price: S$380");
                    System.out.println("(5) Suite (2 persons) - Price: S$500\n");
                    System.out.println("Enter Room types (Enter '1' to '5')");
                    choice=input.nextInt();
                    while(choice>5){
                        if(choice>5){
                        System.out.println("Please enter number between '1' to '5'!");
                        choice=input.nextInt();
                        }                   
                    }

                    String roomTypess= roomType[choice-1];
                    roomAndDate.add(roomTypess);
                    int storePricee = priceRoom[choice-1];
                    all.add(storePricee);

                    System.out.println("Number of rooms required (maximum 10): ");
                    choice=input.nextInt();
                    while(choice>10){       
                        if(choice>10){
                        System.out.println("Please enter again!");
                        choice=input.nextInt();
                        }
                    }
                    all.add(choice);

                    for(int i=0;i<choice;i++){
                        System.out.println("Enter the date of checked-in (dd/mm/yy) for "+roomAndDate.get(0) +" " + (i+1));
                        choiceStr = input.nextLine();
                        choiceStr = input.nextLine();
                        roomAndDate.add(choiceStr);
                        System.out.println(roomAndDate);
                        System.out.println("Enter number of Add-on for "+roomAndDate.get(0) + " " +(i+1)+": ");
                        choice=input.nextInt();
                        while(choice>4){
                        if(choice>4){   
                            System.out.println("Please enter again! Choose only option 1 to 4");
                            choice=input.nextInt();
                            }                           
                        }
                        all.add(choice);            
                        System.out.println("Number of night(s) required (maximum 30) for "+roomAndDate.get(0) + " " +(i+1)+ ": ");
                        choice=input.nextInt();
                        while(choice>30){   
                            if(choice>30){
                            System.out.println("Please enter again! Maximum is 30 days!");
                            choice=input.nextInt();
                            }
                        }           
                        all.add(choice);


                    }
                    new RoomSelection();

                    break;          
        case 'b':       System.out.println("Room req is empty? : "+all.isEmpty());
                        System.out.println("Add-Ons");
                        System.out.println("=====================================================");
                        System.out.println("(1) Breakfast voucher (1 person) per day - Price: S$25");
                        System.out.println("(2) Spa voucher (1 person) - Price: S$60");
                        System.out.println("(3) Half Day Tour voucher (1 person) - Price: S$70");
                        System.out.println("(4) Full Day Tour voucher (1 person) - Price: $100\n");



                        for(int i=0;i<(Integer)all.get(3);i++){

                        System.out.println("Enter Add-On option");
                        choice=input.nextInt();
                        while(choice>4){
                            if(choice>4){   
                                System.out.println("Please enter again! Choose only option 1 to 4");
                                choice=input.nextInt();
                            }                           
                        }
                        all.add(choice);
                        System.out.println("Enter quantity required for Add-On option " + (i+1)+": ");
                        choice=input.nextInt();
                        all.add(choice);
                        }

                    break;
        case 'c':   new MainPage1();break;
        default:continue;
    }

}

}

3 个答案:

答案 0 :(得分:3)

这就是你应该使用泛型的原因。目前,在查看all的声明时,编译器和读者都无法看到其中的含义。看看代码,它似乎是字符串和整数的混合。这是灾难的秘诀。它首先代表的真正意义是什么? 为什么它包含字符串和整数的混合?

听起来很可能你想要一个包含所有这些信息的Room类型,并且all应该像这样声明:

List<Room> allRooms = new ArrayList<Room>();

此外,此方法目前方式太长。当一条线超过约20行但是130+时,我会感到紧张?伊克!确定可以提取哪些位 - 不影响其余代码的位。

哦,了解数组初始值设定项,转而:

Integer[] priceAdd = new Integer[4]; //Initialize a array for add-on prices
priceAdd[0]=25; //To be added to array add-on price
priceAdd[1]=60; //To be added to array add-on price
priceAdd[2]=70; //To be added to array add-on price
priceAdd[3]=100; //To be added to array add-on price      

进入这个:

int[] addOnPrices = { 25, 60, 70, 100 };

(我看不出有什么理由说他们需要Integer而不是int,尽管你现在假设你永远不需要小数值...)

答案 1 :(得分:1)

您需要像这样定义ArrayList的类型:

ArrayList<String> all = new ArrayList<String>();

或使用您正在使用的任何非原始数据类型。

答案 2 :(得分:1)

List<Integer> all = new ArrayList<Integer>();

以上将始终接受并返回整数值。