如何将值放在也在数组中的数组中,或者从另一个数组中为数组赋值?

时间:2015-10-01 12:41:52

标签: java arrays loops indexoutofboundsexception

这是我想要放到另一个数组的array

    String [][]roomType = new String[4][4];
    roomType[0][0] = "Standard";
    roomType[0][1] = "500";
    roomType[0][2] = "5";
    roomType[0][3] = "1";

    roomType[1][0] = "Double";
    roomType[1][1] = "800";
    roomType[1][2] = "4";
    roomType[1][3] = "2";

    roomType[2][0] = "Matrimonial";
    roomType[2][1] = "3500";
    roomType[2][2] = "6";
    roomType[2][3] = "3";

    roomType[3][0] = "Triple";
    roomType[3][1] = "4500";
    roomType[3][2] = "5";
    roomType[3][3] = "4";



    /*-----------------------------------------------------------------
     * Costumer's Information
     -----------------------------------------------------------------*/
    do{//Start Of First Loop - Customer's Info
    System.out.print("\nEnter Number Of Records : ");
    int Records = Integer.parseInt(br.readLine());

    String [][] customer = new String [Records][7]; //records for customer

    for (int x = 0; x < Records; x++){ // Start For Records

    System.out.print("\nConfimation Number: ");
    customer[x][0] = br.readLine();
    System.out.print("\nFirst Name: ");
    customer[x][1] = br.readLine();
    System.out.print("Last Name: ");
    customer[x][2] = br.readLine();
    System.out.print("Guest: ");
    customer[x][3] = br.readLine();
    System.out.print("Night: ");
    customer[x][4] = br.readLine();
    System.out.println();




        System.out.print("1. Standard..............................................P500.00\n");
        System.out.print("2. Double................................................P800.00\n");
        System.out.print("3. Matrimonial...........................................P3,500.00\n");
        System.out.print("4. Triple................................................P4,500.00 \n");

        System.out.print("\n\nPlease Select Room Type: ");
        int SwitchOne = Integer.parseInt(br.readLine());
        int two = SwitchOne;
        switch(SwitchOne){
            case 1:                 
            case 2:
            case 3:
            case 4:         

这是将数组放到另一个数组或从数组赋值给数组的过程。

                    for(int row=SwitchOne-1;row<4;row++){
                        int roomID = Integer.parseInt(roomType[row][3]);
                        if(two == roomID){

                            double price = Double.parseDouble(roomType[row][1]);
                            int available = Integer.parseInt(roomType[row][2]);
                            available -= 1;
                            String avail = Double.toString(available);
                            roomType[row][2] = avail;
                            customer[x][5] = roomType[row][0];

                            double guest = Double.parseDouble(customer[x][3]);
                            guest *= GuestRate;
                            double night = Double.parseDouble(customer[x][4]);
                            price *= night;
                            double totalAmount = guest + price;
                            String AmountTotal = Double.toString(totalAmount);

                            customer[x][6] = AmountTotal;

                        }
                    }

问题是当第一个已放置的数组无法再次使用时。编译器说它OutOfBounds

所以我不能选择以前选择的东西。

我是Java课程的新手,请给我一些帮助。

这只是1个方法类。 我无法理解2种或更多种方法。

如果您不理解,请发表评论,这太难以解释。

2 个答案:

答案 0 :(得分:0)

如果要将两个维度数组从一个维度分配给另一个维度。

请按照以下代码段

进行操作
String[][] newArray = new String[roomType[0].length][roomType[1].length];

        for (int i = 0; i < roomType[0].length; i++){
            for (int j = 0; j < roomType[1].length; j++){
                newArray[i][j] = roomType[i][j];
            }
        }

如果你想要交叉检查,试试这个,无论数组是否正确分配,请尝试下面的

for (int i = 0; i < newArray[0].length; i++){
            for (int j = 0; j < newArray[1].length; j++){
                System.out.print(newArray[i][j] + " ");
            }
            System.out.println("");
        }

答案 1 :(得分:0)

试试这个,

import java.io.*;

public class ArrayTest {
public static void main(String args[]) throws NumberFormatException,
        IOException {
    double GuestRate = 100;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String[][] roomType = new String[4][4];
    roomType[0][0] = "Standard";
    roomType[0][1] = "500";
    roomType[0][2] = "5";
    roomType[0][3] = "1";

    roomType[1][0] = "Double";
    roomType[1][1] = "800";
    roomType[1][2] = "4";
    roomType[1][3] = "2";

    roomType[2][0] = "Matrimonial";
    roomType[2][1] = "3500";
    roomType[2][2] = "6";
    roomType[2][3] = "3";

    roomType[3][0] = "Triple";
    roomType[3][1] = "4500";
    roomType[3][2] = "5";
    roomType[3][3] = "4";

    /*-----------------------------------------------------------------
     * Costumer's Information
     -----------------------------------------------------------------*/
    // Start Of First Loop - Customer's Info
    System.out.print("\nEnter Number Of Records : ");
    int Records = Integer.parseInt(br.readLine());

    String[][] customer = new String[Records][7]; // records for customer
    int available;

    for (int x = 0; x < Records; x++) { // Start For Records

        System.out.print("\nConfimation Number: ");
        customer[x][0] = br.readLine();
        System.out.print("\nFirst Name: ");
        customer[x][1] = br.readLine();
        System.out.print("Last Name: ");
        customer[x][2] = br.readLine();
        System.out.print("Guest: ");
        customer[x][3] = br.readLine();
        System.out.print("Night: ");
        customer[x][4] = br.readLine();
        System.out.println();
        System.out
                .print("1. Standard..............................................P500.00\n");
        System.out
                .print("2. Double................................................P800.00\n");
        System.out
                .print("3. Matrimonial...........................................P3,500.00\n");
        System.out
                .print("4. Triple................................................P4,500.00 \n");

        System.out.print("\n\nPlease Select Room Type: ");
        int SwitchOne = Integer.parseInt(br.readLine());
        int two = SwitchOne;

        int row = SwitchOne - 1;
        int roomID = Integer.parseInt(roomType[row][3]);
        if (two == roomID) {

            double price = Double.parseDouble(roomType[row][1]);

            available = Integer.parseInt(roomType[row][2]);

            if( Integer.parseInt(roomType[row][2]) >= Integer.parseInt(customer[x][3]))
            {
                available = available-Integer.parseInt(customer[x][3]);
            String avail = Integer.toString(available);
            roomType[row][2] = avail;
            customer[x][5] = roomType[row][0];
            double night = Double.parseDouble(customer[x][4]);
            double guest = Double.parseDouble(customer[x][3]);

            guest *= GuestRate * night;

            price *= night;
            double totalAmount = guest + price;
            String AmountTotal = Double.toString(totalAmount);

            customer[x][6] = AmountTotal;
            System.out.println(customer[x][6]);
            }
            else
                System.out.println("Room is not available for "+ customer[x][3]+ " Guests : We have only "+available+  " vacancy in that room");

        }

    }
}

}

它正在工作..输出是,

输入记录数:3

Confimation Number:1

名字:f 姓氏:g 嘉宾:2 晚上:2

  1. 标准.............................................. P500.00
  2. 双.............................................. ..P800.00
  3. 婚姻........................................... P3,500.00
  4. 三重.............................................. ..P4,500.00
  5. 请选择房间类型:1 五 1400.0

    Confimation Number:2

    名字:e 姓氏:r 嘉宾:2 晚上:2

    1. 标准.............................................. P500.00
    2. 双.............................................. ..P800.00
    3. 婚姻........................................... P3,500.00
    4. 三重.............................................. ..P4,500.00
    5. 请选择房间类型:1 3 1400.0

      Confimation Number:3

      名字:d 姓氏:g 嘉宾:2 晚上:2

      1. 标准.............................................. P500.00
      2. 双.............................................. ..P800.00
      3. 婚姻........................................... P3,500.00
      4. 三重.............................................. ..P4,500.00
      5. 请选择房间类型:1 1 客房不适合2位客人入住:该客房仅有1个空位