黑马程序员-java基础2

——-android培训java培训期待与您交流!———-

1. 程序流程控制

首先要对块有个概念理解。块是指一对花括号括起来的若干条简单的Java语句。如main函数所括起来的语句:

    public static void main(String[] args) {
        int n;
        ....
        {
            int j;
            ....
        }
    }

a.条件语句

if语句
格式为:

if (条件表达式) {
            语句块
        }

例子:

//定义两个变量j、i并赋值
        int j =1;
        int i = 0;
        //if语句块
        if(j > i)
        {
            int x = 2014;
        }

走向流程图如下:
ifworkflow.jpg

if else语句
格式为:

        if (条件表达式) {
            语句块1
        }
        else {
            语句块2
        }

例子:

        int age = 13; 
        if (age > 18) {
            System.out.println("恭喜你!");
            System.out.println("你已经成年了!");
        }
        else {
            System.out.println("抱歉!");
            System.out.println("你还没有达到法定年龄!");
        }

注意:else语句跟最近的if语句组成一对
流程图控制图:
ifelse.jpg

if else if语句
格式为:

    if (条件表达式1) {
        语句块1
    }
    else if(条件表达式2){
        语句块2
    }

例子:

        int age = 13;
        if (age > 18) {
            System.out.println("恭喜你!");
            System.out.println("你已经成年了,可以参与这个游戏了!");
        }
        else if(age > 48){
            System.out.println("抱歉!");
            System.out.println("你的年龄有些大了!");
        }
        else if(age == 18){
            System.out.println("你的年龄刚好合适!");
        }
        else {
            System.out.println("非常抱歉!!");
            System.out.println("你的年龄不适合这个游戏的参与条件!");
        }

流程控制图:

ifelseif.jpg

b.循环

while语句
格式为:

        while(条件表达式)
        {
            语句块
        }

注意:只要条件满足,就执行语句块否则不执行
例子:

        int age = 0while(age > 18)
        {
            System.out.println("你的年龄适合参加这个游戏资格!");
        }

流程图:
while.jpg

do while语句
格式为:

        do{
            语句块
        }
        while(条件表达式);

注意:不管条件表达式是否成立,语句块都要执行直到条件表达式成立。
例子:

        int i = 0;
        do {
            System.out.println("i = " + i);
            i++;
        }while (i < 3);

流程图:
dowhileflow.png

for语句
格式为:

        for(初始化表达式;循环条件表达式;循环后的操作表达式)
        {
            语句块
        }

例子:

        for(int x = 0; x < 3; x++)
        {
            System.out.println("x = " + x);
        }

流程图:
forflow.png

switch语句
格式为:

        switch(表达式) //表达式接受的值主要有四类分别为:int、byte、short、char、String类型和枚举类型
        {
            case 取值1:
                执行语句;
                break;
            case 取值2:
                执行语句;
                break;
            ...
            case 取值n:
                执行语句;
                break;
            default:
                执行语句;
                break;

注意case的顺序是无序的。直到碰到default语句之后出现的break结束语句执行
例子:

        int x = 3;
        switch(x) //表达式接受的值主要有四类分别为:int、byte、short、char
        {
            case 1:
                System.out.println("输入不准确");
                break;
            case 2:
                System.out.println("输入不准确");
                break;
            case 3:
                System.out.println("输入准确");
                break;
            default:
                System.out.println("输出默认值");
                break;
        }

流程图:
switchflow.png

c.其他循环控制语句

break(跳出循环)
作用范围:应用于选择结构和循环结构语句。
continue(结束本次循环继续下次循环)
作用范围:应用于循环结构语句。

2.函数

a.函数的定义

  • 定义:函数就是定义在类中的具有特定功能的一段独立小程序。函数也称为方法。
  • 函数定义格式:
    修饰符 返回值类型 函数名(参数类型 形式参数1, 参数类型 形式参数2,参数类型 形式参数3...)
    {
        执行语句;
        return 返回值;
    }

1.返回值类型:函数运行后的结果的数据类型
2.参数类型:形式参数的数据类型
3.形式参数:一个变量,用于存储调用函数时传递给函数的实际参数。
4.实际参数:传递给形式参数的具体数值
5.return:用于结束函数
6.返回值:该值会返回给调用者

b.函数的特点

  • 函数可以将功能代码进行封装,以后也可以对此功能进行重复使用。函数只有被调用才会被执行,同时也提高了代码的复用性。
  • 注意:函数中只能调用函数,不可以在函数内部定义函数;函数的结果应该返回给调用者,让调用者来处理。当函数运算后,没有具体的返回值时,这时可以用一个关键字void(函数没有具体返回值类型)来代替返回值类型,当有这个关键字时,return语句可以省略不写,写上也可以。
    比如:
     //比较两个数值的大小,并获取最大值
    public static void getMax(int m, int n)
    {
        if(m > n)
        {
            System.out.println(m);
        }else
        {
            System.out.println(n);
        }
    }

c.函数的应用

  • 将程序中需要用到的功能写成一个函数,以备其他函数调用。
  • 定义一个函数的要点:明确函数返回值类型;明确函数的参数列表。
    /**
     * @param list a list of integers
     * @return the largest number in the given list
     */
    public static int largest(int[] list)
    {
        int index, max = Integer.MAX_VALUE;
        //获取列表值并与max数比较得出结果
        for(index = 0; index < list.length - 1; index++)
        {
            if(list[index] > max ){
                max = list[index];
            }
        }
        //返回结果值
        return max;
    }

d.函数的重载

  • 概念:在同一个类中,允许存在一个以上的同名函数,只要它们的参数个数或者参数类型不同。
  • 特点:与返回值类型无关,只看参数列表。
  • 示例:
    /**
     * 返回两个数的最大值
     * @param m
     * @param n
     * @return max
     */
    public static int getMax(int m, int n) {
        if (m > n) {
            return m;
        } else {
            return n;
        }
    }
    /**
     * 返回三个数的最大值
     * @param x
     * @param y
     * @param z
     * @return getMax
     */
    public static int getMax(int x, int y, int z)
    {
       return getMax(x,y)>z?getMax(x,y):z;
    }
void show(int a ,char b,double c){}
1. void show(int x, char y, double z){}//没有重载  与原函数一样
2.int show(int a, double c,char b){}//重载了   参数列表不同
3.void show (int a, double c,char b){}//重载了  参数列表不同 同上 
4.boolean show (int c, char b)//重载了   参数列表不同
5.void show(double c)//重载了  参数个数不同
6.double show(int x, char y,double z)//没有重载 参数一致
3.数组

a.数组的定义

  • 同一种类型数据的集合。
  • 数组元素从0开始编号,方便操作这些元素。
  • 定义格式:
    元素类型[] 数组名 = new 元素类型[元素个数或数组长度];
    int[] arr = new int[5];
    元素类型[] 数组名 = new 元素类型[]{元素,元素,...};
    int[] arr = new int[]{3,5,6};
  • 定义的数组默认初始值
    数值元素初始值为0
    布尔元素初始值为false
    对象数组元素初始值为null

b.数组的内存分配及特点

  • 在java中内存结构为:寄存器(我们在程序中无法控制)、堆(存放new产生的对象数据)、栈(存放基本类型的数据和对象的引用,但对象本身不存在栈中,而是存放在堆中)、静态域(存放在对象中用static定义的静态成员)、常量池(存放常量)、非RAM存储(磁盘等永久存储空间)
  • 数组的内存分配为:
    int[] arr = new int[3];声明一个整型数组arr,里面存有3个整数元素。声明的数组arr存放在栈中,而new等于在内存开辟一块内存区域也就是堆,用来存放new对象的数据。栈arr是通过堆中的内存地址引用到此对象。

c.数组的操作和常见问题

  • 数组静态初始化:
    int[] arrs = {2,4,5,6};或者new int[]{3,6,5};为每个数组制定大小和初始值。
  • 数组遍历:

方式一(forech循环):

        //定义数组arrars
        int[] arrays = new int[]{3,5,6,7,34};
       //forech获取数组每个元素
        for(int element : arrs)
        {
            System.out.print(element + " ");
        }

方式二(通过数组的属性length来获取数组的长度然后使用for循环获取数组元素):

         //定义数组arrays
        int[] arrays = new int[]{3,5,6,7,34};
        //使用for循环获取数组中的每个元素
        for(int i = 0; i < arrays.length; i++)
        {
            System.out.print(arrays[i]);
        }
  • 数组中的最值:
    获取最小值
public class DateDemo {
    public static void main(String[] args) {
        //定义数组并初始化初值,最后打印
        double[] arrays = new double[] {3, 5, 6, 7, 34};
        System.out.print(getMax(arrays));
    }

    /**
     * 获取数组中的最小值
     * @param arr 数组
     * @return min 数组中的最小值
     */
    public static int getMax(int[] arr) {
        int min = 0;
        for (int x = 1; x < arr.length; x++) {
            if (arr[x] < arr[min]) {
                min = x;
            }
        }
        return arr[min];
    }

获取最大值:

public class DateDemo2{
    public static void main(String[] args) {
        //定义数组并初始化初值,最后打印
        int[] arrays = new int[] {3, 5, 6, 7, 34};
        System.out.print(getMax(arrays));
    }

    /**
     *返回数组中的最大值
     * @param arr 定义的数组
     * @return max 返回数组中的最大致
     */
    public static int getMax(int[] arr)
    {
        int max = 0;
        for(int x = 1; x < arr.length; x++)
        {
            if(arr[x] > arr[max])
            {
                max = x;
            }
        }
        return arr[max];
    }
}
  • 选择排序
    /**
     * 选择排序
     * @param arrays 需要传进来的数组
     */
    public static void selectSort(int[] arrays)
    {
        for(int x = 0; x < arrays.length; x++)
        {
            for(int y = x + 1; y < arrays.length; y++)
            {
                if(arrays[x] > arrays[y])
                {
                    int temp = arrays[x];
                    arrays[x] = arrays[y];
                    arrays[y] = temp;
                }
            }
        }
    }
  • 冒泡排序
    /**
     * 冒泡排序
     * @param arr 需要排序的数组
     */
    public static void bubbleSort(int[] arr)
    {
        for(int x = 0; x < arr.length - 1; x++)
        {
            for(int y = 0; y < arr.length - x - 1; y++)
            {
                if(arr[y] > arr[y+1])
                {
                    int temp = arr[y];
                    arr[y] = arr[y+1];
                    arr[y+1] = temp;
                }
            }
        }
    }
  • 二分查找
    /**
     * 二分查找
     * @param arr 待查询的数组
     * @param key 查询的书
     * @return mid 返回查找的数
     */
    public static int halfSearch(int[] arr, int key)
    {
        int min =0, max = arr.length, mid;
        while (min < max)
        {
            mid = (max + min) / 2;
            if(key > arr[mid])
            {
                min = mid + 1;
            }else if(key < arr[mid])
            {
                max = mid - 1;
            }else
            {
                return mid;
            }
        }
        return -1;
    }

d.二维数组及多维数组

  • 概念:数组中的数组,也就是数组中的元素是数组。
  • 定义格式:
    int[][] arr =new int[3][2];:定义了一个有三个一维数组,每一个一维数组中有2个元素的二维数组arr。
    int[][] arr = new int[3][2];
    //遍历二维数组
    for(int x = 0; x < arr.length; x++)
    {
        for(int y = 0; y < arr[x].length; y++)
        {
            System.out.println(arr[x][y]);
        }
    }

e.进制转换

  • 十进制转换成二进制
    /**
     * 十进制转换成二进制
     * @param num 需要被转换的十进制数
     */
    public static void toBin(int num)
    {
        StringBuffer sb = new StringBuffer();

        while(num > 0)
        {
            sb.append(num % 2);
            num = num / 2;
        }
        //对结果顺序进行反转
        System.out.println(sb.reverse());
    }
    /**
     * 查表法十进制转二进制
     * @param num 待转换的数
     */
    public static void toBin2(int num)
    {
        char[] chs = {'0','1'};

        //临时容器用int的容量装char类型数组容量
        char[] arr = new char[32];
        int pos = arr.length;
        while (num != 0)
        {
            int temp = num & 1;
            arr[--pos] = chs[temp];

            num = num >>> 1;
        }

        for(int x = pos; x < arr.length; x++)
        {
            System.out.print(arr[x]);
        }
    }
  • 十进制转换成十六进制
    /**
     * 十进制转换成十六进制
     * @param num 待转换的数
     */
    public static void toHex(int num)
    {
        StringBuffer sb = new StringBuffer();
        //十六进制是8个四位表示
        for(int i = 0; i < 8; i++)
        {
            //与15进行与运算
            int temp = num & 15;
            if(temp > 9)
            {
                sb.append((char)(temp - 10 + 'A'));
            }else
            {
                sb.append(temp);
            }

            num = num >>> 4;
        }
        System.out.println(sb.reverse());
    }
    /**
     * 查表法进行十进制转十六进制
     * @param num 待转换的数
     */
    public static void toHex2(int num)
    {
        char[] chs = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
//
//        for(int x = 0; x < 8; x++)
//        {
//            int temp = num & 15;
//            System.out.println(chs[temp]);
//            num = num >>> 4;
//        }
        //定义一个临时容器
        char[] arr = new char[8];
        int pos = arr.length;//操作数组的指针

        while(num != 0)
        {
            int temp = num & 15;
            arr[--pos] = chs[temp];
            num = num >>> 4;
        }

        //存储数据的arr数组遍历
        for(int x = pos; x < arr.length; x++)
        {
            System.out.print(arr[x] + ",");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值