从Array索引中删除0

时间:2015-02-17 01:20:04

标签: java arrays

当按顺序排列所有房间时(使用底部的orderRooms方法),数组会自动为房间0指定一个名称。我希望房间0始终为空,并指定一个“e”作为其名称(表示它是空的)。

public class Hotel {
private static String[] hotel= new String[11];

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws Exception {
    String command;
    Scanner input = new Scanner(System.in);
    String roomName;
    int roomNum = 1;


    initialise();   

    while (roomNum<11 && roomNum>=1)

    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter command : ");
        command = input.next();
        command = command.toLowerCase();

        if (command.charAt(0) == 'a') {
            addcustomer();
        }

        if (command.charAt(0) == 'v') {
            viewoccupants();
        }

        if (command.charAt(0) == 'e') {
            emptyrooms();  
        }

        if (command.charAt(0) == 'd') {
            deleteroom();  
        }    

        if (command.charAt(0) == 'f') {
            findroom();  
        }   

        if (command.charAt(0) == 's') {
            storedata();  
        } 

        if (command.charAt(0) == 'l') {
            loadData();  
        }

        if (command.charAt(0) == 'o') {
            orderRooms();  
        }
    }
}

 private static void initialise() {
     for (int x = 0; x < 11; x++ ) hotel[x] = "e";
     System.out.println( "initilise ");
  }

private static void viewoccupants() {
         for (int x = 0; x < 11; x++ )
          {
           System.out.println("room " + x + " occupied by " + hotel[x]);
         }
}

private static void addcustomer() {
    String roomName;
    int roomNum;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter room number (1-10) or 0 to stop:" ) ;
    roomNum = input.nextInt();
    if (roomNum<11) {
        System.out.println("Enter name for room " + roomNum +" :" ) ;
        roomName = input.next();
        roomName = roomName.toLowerCase();
        hotel[roomNum] = roomName ;   
    }
    else {
        System.exit(0);
    }
}

private static void emptyrooms() {
    for (int x = 0; x < 11; x++ )
          {
           if (hotel[x].equals("e"))System.out.println("room " + x + " is empty");
         }
}

private static void deleteroom() {
    String x = "e";
    int roomNum;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter room to be vacated: " );
    roomNum = input.nextInt();
    if (roomNum<11) {
        hotel[roomNum] = x;   
    }
    else {
        System.exit(0);
    }
}

private static void findroom() {
    String roomName;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter name: " ) ;
    roomName = input.next();
    roomName = roomName.toLowerCase();
    for(int i = 0; i < hotel.length; i++){
        if(hotel[i].equals(roomName)){
            System.out.println(roomName + " is located in room " + i);
        }
    }
}

private static void storedata () throws Exception {
    FileWriter writer = new FileWriter("HotelArray.txt");
    for(String str: hotel) {
        writer.write(str);
        writer.write("\r\n");
    }
    writer.close();
    System.out.println("File 'HotelArray.txt' saved to C:\\Users\\Ganz\\Documents\\NetBeansProjects\\Hotel");
}

private static void loadData() throws IOException {
    int lineCount = 1;
    try {                             
        Scanner sc = new Scanner(new BufferedReader(new FileReader("C:\\Users\\Ganz\\Documents\\NetBeansProjects\\Hotel\\HotelArray.txt")));
        String fileLine;
        while (sc.hasNext()) {
            fileLine = sc.nextLine();
            hotel[lineCount-1] = fileLine;
            lineCount++;
        }
        sc.close();
        System.out.println("File 'HotelArray.txt' loaded from C:\\Users\\Ganz\\Documents\\NetBeansProjects\\Hotel");
    }
    catch (IOException e) {
    }
}

private static void orderRooms() {
    Arrays.sort(hotel);
    for(int i = 0; i < hotel.length; i++)
        System.out.println(hotel[i]);
  }
}

1 个答案:

答案 0 :(得分:1)

要对除第一个元素之外的所有元素进行排序:

Collections.sort(Arrays.asList(hotel).subList(1, hotel.length));

如果hotel已经是List,这会稍微简单一点。如果你没有理由不这样做,你应该考虑使用列表。

说明:Arrays.asList(hotel)返回List,这是&#34;视图&#34;数组。 subList会返回List,这是一个&#34;视图&#34;原始列表的一部分 - 在这种情况下,从第二个元素到结尾。

通过&#34;查看&#34;我的意思是修改返回的List实际上修改了原始List /数组 - 所以当Collections.sort对其进行排序时,它实际上对hotel数组的那一部分进行排序