集合1 Collection(存储引用)

2017-08-03  本文已影响15人  雷一凡

1,一个一个存储 Collection

     一对一对存储Map

List 和 Set

Collection 的一些方法:

Collection ;自动装箱在集合中 存储在集合中对象都要重写equals 方法

it.hasnext 不会向下 移动,it.next 才会向下移动

add 重写了toString 方法 

remove 也可以通过迭代器删除所有数据

删除推荐使用迭代器自身的方法删除!。

ArrayList 初始化容量10 增加扩容时则增加1.5倍

Vector 初始化容量为10 增大扩容则是增加2倍

Room

public class Room {

private String no;

private String type;//标准  双人  豪华

private boolean isUse;

public Room(String no, String type, boolean isUse) {

super();

this.no = no;

this.type = type;

this.isUse = isUse;

}

public String getNo() {

return no;

}

public void setNo(String no) {

this.no = no;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public boolean isUse() {

return isUse;

}

public void setUse(boolean isUse) {

this.isUse = isUse;

}

public String toString(){

return '['+no+']'+'['+type+']'+(isUse?"占用":"空闲")+" ";

}

}

-------------------------------------------

Hotel

public class Hotel {

Room [][]r;

Hotel(){

r=new Room[5][10];

for (int i = 0; i < r.length; i++) {

for (int j = 0; j < r[i].length; j++) {

if(0==i||1==i){

r[i][j]=new Room(((i+1)*100)+j+1+"","标准间",false);

}

if(2==i||3==i){

r[i][j]=new Room(((i+1)*100)+j+1+"","双人间",false);

}

if(4==i){

r[i][j]=new Room(((i+1)*100)+j+1+"","豪华间",false);

}

}}}

public void print(){

for (int i = 0; i < r.length; i++) {

for (int j = 0; j < r[i].length; j++) {

System.out.print(r[i][j]+"    ");

}

System.out.println();

}

}

public void order(String no){

for (int i = 0; i < r.length; i++) {

for (int j = 0; j < r[i].length; j++) {

if(r[i][j].getNo().equals(no)){

r[i][j].setUse(true);

return;

}

}

}

}

}

-------------------------------------------------------------

main

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Hotel h = new Hotel();

h.print();

while(true){

System.out.println("請輸入預定房間");

Scanner s = new Scanner(System.in);

String no =s.next();

h.order(no);

h.print();

}

}

}

上一篇下一篇

猜你喜欢

热点阅读