Java入门四步走

Java入门四步学习指南

1. 简单的入门语法:

1.1 数据类型:

基本数据类型:
整数类型 —— byte、short、int、long,
浮点类型 —— float、double
字符类型 —— char
布尔类型 —— boolean

引用数据类型:
接口(interface)、数组([ ])、类(class)。


1.2 循环怎么写:

1) 顺序控制

2) 分支控制:
        1) 单分支 if; 2) 双分支 if-else; 3) 多分支 if-elseif-....-else;4)switch 分支结构

3) 循环控制:for循环;while循环;do..while循环

1.3 数组怎么定义:

先声明数组:语法: 数据类型 数组名[]; 也可以 数据类型[] 数组名;        int a [ ]; 或者 int[ ] a;

创建数组:    语法: 数组名=new 数据类型[大小];        a = new  int[10];

1.4 函数是怎么写的

修饰符 返回值类型 函数名称(参数类型 参数1,参数类型参数2, . . . ){
                函数执行代码;
                return 返回值; 
        }

public static int addFunc (int a,int b) {
                 return a+b;
         }        

返回值类型:函数运行后的结果类型
参数类型:是形式参数的数据类型
形式参数:是一个变量,由于存储调用函数时传递给函数的实际参数
实际参数:传递给函数的具体值
return:用于结束函数
返回值:函数运算后的结果值,该值会返回给该函数的调用者 
注意:当函数没有返回值类型是用void来表示
如果返回值类型是void,name函数中的return语句可以省略不写

2.用这个编程语言实现基础的数据结构:

2.1 顺序表:

顺序表的实现:

public class SeqList{
 	private int length;//表长
    private int[] datas;//数据
}

顺序表的创建和初始化:

	/**
     * 创建空的顺序表
     */
    public SeqList() {
    }

    /**
     * 创建一个指定容量的顺序表
     *
     * @param capacity
     */
    public SeqList(int capacity) {
        length = 0;
        datas = new int[capacity];
    }

    /**
     * 构造指定容量和指定数组元素的顺序表
     * @param capacity
     * @param array
     */
    public SeqList(int capacity, int[] array) {
        if (capacity < array.length) {
            throw new IndexOutOfBoundsException();
        } else {
            length = 0;
            datas = new int[capacity];
            for (int i = 0; i < array.length; i++) {
                datas[i] = array[i];
                length++;
            }
        }
    }

    /**
     * 初始化表的最大容量
     *
     * @param capacity
     */
    public void create(int capacity) {
        if (datas != null) {
            throw new RuntimeException();
        } else {
            length = 0;
            datas = new int[capacity];
        }

    }

获取指定下标的元素:

   /**
     * 获取指定下标的元素
     *
     * @param index
     * @return
     */
    public int get(int index) {
        if (index <= length && index >= 0) {
            return datas[index];
        } else {
            throw new IndexOutOfBoundsException();
        }
    }

指定位置插入元素:

  /**
     * 指定位置插入元素
     *
     * @param index
     * @param element
     */
    public void insert(int index, int element) {
        if (index >= length || index < 0) {
            throw new IndexOutOfBoundsException();
        } else {
            if (length + 1 <= datas.length) {
                for (int i = length; i > index; i--) {
                    datas[i] = datas[i - 1];
                }
                datas[index] = element;
                length++;
            } else {
                System.out.println("表已满,无法插入");
            }
        }
    }

删除指定下标元素:

    /**
     * 删除指定下标元素
     *
     * @param index
     */
    public void delete(int index) {
        if (index < 0 || index >= length) {
            throw new IndexOutOfBoundsException();
        } else {
            for (int i = index; i < length - 1; i++) {
                datas[i] =
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值