//printArray({1,2,3})错误
printArray(new int[]{1,2,3});
以后一般有界面
javac HelloWorld.java admin 123
String username = args[0];
String password = args[1];
数组长度确定不可改变,满了需要建一个大容量的数组,将小数组中的数据拷贝至大数组中
int[] src= {1,2,3,4};
int[] dest = new int[20];
System.arraycopy(src,srcPos:0,dest,destPos:0,length:4);
左边数字与右边比较,大于则交换位置,每一次循环找出最大的数据,放在参与比较的数据右边
int[] arr = {21,2,33,4,333,46,5};
for(int i =arr.length-1;i>0;i--){
for(int j=0;j<i;j++){
if(arry[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
找到参与比较的数据的最小值放到最左边
int[] arr = {21,2,33,4,333,46,5};
for(int i=0;i<arr.length-1;i++){
int min = i;
for(int j=i+1;j<arr.length;j++){
if(arr[j]<arr[min]){
min = j;
}
}
if(min !=i){
int temp = arr[min];
arr[min] arr[i];
arr[i]=temp;
}
}
二分法查找建立在排序的基础上
Arrays工具类中已有
int[] arr = {1,44,57,78,92,104,221}
public static int binarySearch(int[] arr,int dest){
int begin = 0;
int end = arr.length-1;
while(begin<=end){
int mid = (begin + end)/2;
if(arr[mid]==dest){
return mid;
}else if(arr[mid]<dest){
begin = mid+1;
}else{
end = mid -1;
}
}
return -1;
}
int arr = {3,5,22,6,333,2,75,43};
Arrays.sort(arr);
int index = Arrays.binarySearch(arr,22);
System.out.println(index ==-1?"该元素不存在":"该元素下标是:"+ index);
public class MyStack {
private Object[] elements;
private int index;
public MyStack() {
this.elements =new Object[10];
this.index = -1;
}
public void push(Object obj){
if(index>=elements.length-1){
System.out.println("压栈失败,栈已满");
return;
}
index++;
elements[index]=obj;
System.out.println("压栈"+obj+"成功,栈帧指向"+index);
}
public void pop(){
if(index<0){
System.out.println("栈已空");
return;
}
System.out.println("弹栈"+elements[index]+"元素成功");
index--;
System.out.println("栈帧指向"+index);
}
public Object[] getElements() {
return elements;
}
public void setElements(Object[] elements) {
this.elements = elements;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
public class MyStackTest {
public static void main(String[] args) {
MyStack stack = new MyStack();
stack.push(new Object());
stack.pop();
}
}
public class Room {
private int no;
private String type;
private boolean status;
public Room(){ }
public Room(int no, String type, boolean status) {
this.no = no;
this.type = type;
this.status = status;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public boolean equals(Object obj){
if((obj==null)&&!(obj instanceof Room)) return false;
if(this==obj) return true;
Room room=(Room)obj;
return this.getNo()==room.getNo();
}
public String toString(){
return "["+no+","+type+","+(status?"占用":"空閑")+"]";
}
}
public class Hotel {
private Room[][] rooms;
public Hotel(){
rooms = new Room[3][10];
for (int i = 0; i <rooms.length ; i++) {
for (int j = 0; j <rooms[i].length ; j++) {
if(i==0){
rooms[i][j] = new Room((i+1)*100+j+1,"單人間",false);
}else if(i==1){
rooms[i][j] = new Room((i+1)*100+j+1,"雙人間",false);
}else if(i==2){
rooms[i][j] = new Room((i+1)*100+j+1,"總統套房",false);
}
}
}
}
public void print(){
for (int i = 0; i <rooms.length ; i++) {
for (int j = 0; j <rooms[i].length ; j++) {
Room room = rooms[i][j];
System.out.print(room);
}
System.out.println();
}
}
public void order(int roomNo){
Room room = rooms[roomNo/100-1][roomNo%100-1];
room.setStatus(true);
System.out.println(roomNo+"已定房");
}
public void exit(int roomNo){
Room room = rooms[roomNo/100-1][roomNo%100-1];
room.setStatus(false);
System.out.println(roomNo+"已退房");
}
}
import java.util.Scanner;
public class HotelMngSystem {
public static void main(String[] args) {
Hotel hotel = new Hotel();
while (true) {
System.out.println("[1]查看列表,[2]訂房,[3]退房,[0]退出");
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入序號:");
int i = scanner.nextInt();
if (i == 1) {
hotel.print();
} else if (i == 2) {
System.out.println("輸入訂房編號");
int roomNo = scanner.nextInt();
hotel.order(roomNo);
} else if (i == 3) {
System.out.println("輸入退房編號");
int roomNo = scanner.nextInt();
hotel.exit(roomNo);
} else if (i == 0) {
System.out.println("再見");
return;
} else {
System.out.println("編號出錯,重新輸入");
}
}
}
}
上一章:JavaS进阶 第五章 内部类
下一章:JavaSE进阶 第七章 常用类 (一) String