自定义一个顺序表并将其输出

public class Table {
  /*  // 初始化的最大存储容量
    public static final int Size = 5;
    // 存储数据
    public int[] head;
    // 顺序表的长度
    public int length;
    // 顺序表的存储容量
    public int size;

    *//**
     * 初始化顺序表
     * @param t
     *//*
    public static void initTable(Table t){
        // 给顺序表发配动态的存储空间
        t.head = new int[Size];
        // 判断顺序表是否申请失败
        if(t.head == null){
            System.out.println("顺序表申请失败");
            System.exit(0);
        }
        // 初始化顺序表的长度
        t.length = 0;
        // 初始化申请表的内存空间大小
        t.size = Size;
    }
    // 打印顺序表
    public static void displayTable(Table t){
        for(int i = 0; i < t.length; i ++){
            System.out.println(t.head[i] + " ");
        }
    }

    public static void main(String[] args) {
        Table t = new Table();
        initTable(t);
        for(int i = 0; i < Size; i ++){
            t.head[i] = i + 1;
            t.length ++;
        }
        displayTable(t);

    }*/

    public static final int Size = 5;
    public int[] head;
    public int length;
    public int size;

    public static void initTable(Table t){
        t.head = new int[Size];
        if(t.head == null){
            System.exit(0);
        }
        t.length = 0;
        t.size = Size;
    }
    public static void display(Table t){
        for(int i = 0; i < t.length; i ++){
            System.out.println(t.head[i]);
        }
    }

    public static void main(String[] args) {
        Table t = new Table();
        initTable(t);
        for(int i = 0; i < Size; i ++){
            t.head[i] = i + 1;
            t.length ++;
        }
        display(t);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值