java学习初探八之数组应用

1.酒店管理系统的作业

//抽象酒店的房间
public class Room {
    private String no;
    private String type;//标准间,双人间,豪华间
    private boolean isUse;//false表示空闲,true表示占用
    public Room(String no,String type,boolean isUse) {
        super();
        this.no=no;
        this.type=type;
        this.isUse=isUse;
    }
    public String toString() {
        return "{"+no+","+type+","+(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 class Hotel {
    //房间
    Room[][] rooms;
    public Hotel() {
        //5层,每层10个房间
        rooms = new Room[5][10];

        //赋值
        //1,2标准间
        //3,4双人间
        //5 豪华间 
        for (int i = 0; i < rooms.length; i++) {
            for (int j = 0; j < rooms[i].length; j++) {
                if(i==0||i==1) {//标准间
                    rooms[i][j]=new Room((i+1)*100+j+i+"", "标准间", false);
                }
                if(i==2||i==3) {//双人间
                    rooms[i][j]=new Room((i+1)*100+j+i+"", "双人间", false);
                }
                if(i==4) {//豪华间
                    rooms[i][j]=new Room((i+1)*100+j+i+"", "豪华间", false);
                }
            }
        }
    }
    //对外提供一个打印酒店房间列表的方法
    public void print() {
        for (int i = 0; i < rooms.length; i++) {
            for(int j=0;j<rooms[i].length;j++) {
                System.out.print(rooms[i][j]+"");
            }
            System.out.println();
        }
    }
    //对外提供一个预订酒店的方法
    public void order(String no) {
        for (int i = 0; i < rooms.length; i++) {
            for(int j=0;j<rooms[i].length;j++) {
                if(rooms[i][j].getNo().equals(no)) {
                    //将该房间状态改为占用
                    if(rooms[i][j].isUse()==false) {
                        rooms[i][j].setUse(true);
                        System.out.println("预订成功!");
                        return;
                    }else {
                        System.out.println("该房间已被占用,预订失败!");
                        return;
                    }
                }
            }
        }
        System.out.println("您输入的房间号有误");        
    }
}
import java.util.Scanner;

public class ArrayTest01 {

    public static void main(String[] args) {
        Scanner s=new Scanner(System.in) ;
        System.out.println("欢迎使用酒店管理系统,酒店房间列表如下所示");
        //初始化酒店
        Hotel h=new Hotel();
        //输出酒店列表
        h.print();

        while(true) {
            //预订房间
            System.out.println("请输入预订房间的编号: ");
            String no=s.next();
            h.order(no);
            //输出酒店列表
            h.print();  
        }

    }

}

2.使用数组模拟栈

package com.array;
//栈,后进,先出
public class MyStack{
    //使用数组存储数据
    //栈可以存储多个引用类型的元素
    Object[] elements;

    //指向栈顶元素上方的一个帧
    int index;
    //栈默认初始化容量为5
    //constructor
    public MyStack() {
        this(5);
    }
    public MyStack(int max) {
        elements=new Object[max];
    }
    //栈对外应提供一个压栈的方法
    public void push(Object element) throws StackOperationException {
        if(index==elements.length) {
            throw new StackOperationException("栈已满");
        }
        elements[index++]=element;

    }
    //栈对外应该提供一个弹栈的方法
    public Object pop() throws StackOperationException {
        if(index==0) {
            throw new StackOperationException("栈已空");
        }
        return elements[--index];
        }
}
package com.array;
//栈操作异常
public class StackOperationException extends Exception{
    public StackOperationException() {}
    public StackOperationException(String message) {
        System.out.println(message);
    }
}
package com.array;
public class StackTest {
    public static void main(String[] args) {
        MyStack s=new MyStack(); 
        User u1=new User("Jack", 20);
        User u2=new User("Lilei", 21);
        User u3=new User("Wanghong", 22);
        User u4=new User("Tom", 23);
        User u5=new User("Jerry", 24);
        try {
            //压
            s.push(u1);
            s.push(u2);
            s.push(u3);
            s.push(u4);
            s.push(u5);
            s.push(u5);
        } catch (StackOperationException e) {
            e.printStackTrace();
        }

        try {
            //弹
            System.out.println(s.pop());
            System.out.println(s.pop());
            System.out.println(s.pop());
            System.out.println(s.pop());
            System.out.println(s.pop());

        } catch (StackOperationException e) {
            e.printStackTrace();
        }
    }
}
class User{
    String name;
    int age;
    public User(String name,int age) {
    this.name=name;
    this.age=age;
    }
    public String toString() {
        return "User[name="+name+",age="+age+"]";
    }
}
//输出结果:
栈已满
com.array.StackOperationException
    at com.array.MyStack.push(MyStack.java:21)
    at com.array.StackTest.main(StackTest.java:19)
User[name=Jerry,age=24]
User[name=Tom,age=23]
User[name=Wanghong,age=22]
User[name=Lilei,age=21]
User[name=Jack,age=20]

3.冒泡排序

package com.array;
/*
 * int 类型数组 :3 1 6 2 5
 */
public class BubbleSort {
    public static void main(String[] args) {
        int[] a= {3,1,6,2,5};
        for(int i=0;i<a.length;i++) {//外层循环length次
            for(int j=0;j<a.length-1-i;j++) {//每次比较前a.length-1-i
                if(a[j]>=a[j+1]) {//保证大数靠右
                    int tmp=a[j+1];
                    a[j+1]=a[j];
                    a[j]=tmp;
                }
            }
        }
        for(int m=0;m<a.length;m++) {
            System.out.print(a[m]+" ");//冒泡排序后的数组
        }
    }
}
//输出结果
1 2 3 5 6 

4.选择排序
算法:找出最小值,然后把这个最小值和最前面的数据交换位置。

package com.array;
public class SelectSort {
    public static void main(String[] args) {
        int[] a= {3,1,6,2,5};
        //选择排序
        for(int i=0;i<a.length-1;i++) {
            //假设第一个数据是最小值
            //记录最小值元素的下标
            int min=i;
            for(int j=i+1;j<a.length;j++) {
                if(a[min]>a[j]) {
                    min=j;
                }
            }
            //考虑交换位置
            if(min!=i) {
                int tmp;
                tmp=a[min];
                a[min]=a[i];
                a[i]=tmp;
            }
        }

        //输出
        for(int i=0;i<a.length;i++) {
            System.out.print(a[i]+" ");
        }
    }
}

/*
 3 1 6 2 5 下标
 第一次循环      0   
 1 3 6 2 5

 3 6 2 5
 第二次循环      1   
 2 6 3 5

 6 3 5
 第三次循环      2
 3 6 5

 6 5
  第四次循环     3   
 5 6

 */

5二分法查找(折半查找)
(1)二分法查找是建立在已经排序的基础上的;
(2)以下程序,从小到大排序
(3)这个数组中没有重复的元素

package com.array;
/*
    1 3 5 9 11 13 56
    以上是一个已经排好序的int类型的数组,要求快速找出13这个元素的下标 
 */
public class Find {
    public static void main(String[] args) {
        int[] a= {1,3,4,5,7,8,9,10,23,25,29};
        int destElement=100;
        //查找10的下标
        int index=binarySearch(a,destElement);//如果找到则返回元素下标,如果找不到,返回-1
        if(index==-1) {System.out.println(destElement+"该元素不存在");}
        else {System.out.println(destElement+"该元素下标为"+index);}
    }
    //折半查找算法
    public static int  binarySearch(int[] a,int destElement) {
        int begin=0;
        int end=a.length-1;

        while (begin<=end) {
            int mid=(begin+end)/2;
            if(a[mid]==destElement) {return mid;}
            else if (a[mid]>destElement) {
                end=mid-1;
            }else if (a[mid]<destElement) {
                begin=mid+1;    
            }       
        }
        return -1;
    }
}

6.Arrays数组工具类
java.util.Arrays是sun提供的一个工具类
该类主要是针对数组的操作,比如排序、二分查找等。

package com.array;

import java.util.Arrays;

public class ArrayTest07 {
    public static void main(String[] args) {
        int[] a= {3,1,6,2,5};
        //排序
        Arrays.sort(a);
        //输出
        for(int i=0;i<a.length;i++) {
            System.out.print(a[i]+" "); 
        }
        System.out.println();
        //对排好序的数据进行二分查找
        int index=Arrays.binarySearch(a, 6);
        System.out.println("该元素下标是"+index);
        //填充数组
        Arrays.fill(a,0,2,8);//0 1被填充,2没有
        for(int i=0;i<a.length;i++) {
            System.out.print(a[i]+" ");
        }
    }
}
//输出结果
1 2 3 5 6 
该元素下标是4
8 8 3 5 6 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值