JAVA学习笔记

本文介绍了数组的基本操作,包括获取最大值、最小值、排序等,并详细讲解了不同进制之间的转换方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

内存:

栈内存:数据使用完毕,会自动释放内存。

堆内存:new出来的实体对象,都在堆内存中。垃圾回收机制。数组,对象。如:new int[3] 生成3个格子,[0]    [1]   [2]

数组名存放在栈内存中,引用堆内存中数组。

默认初始化值:默认 int:0  double:0.0  float:0.0f  boolean:false.

x = null; 引用数据类型用null,当实体类型在堆内存中没有引用,会启动垃圾回收机制,在堆内存中清除。

 

ArrayIndexOutOfBoundsException:脚标不存在

NULLPointerException:空指针异常

打印数组:

    //打印数组
    public static void printArray(int[] arr){
        System.out.print("[");
        for(int x=0;x<arr.length;x++){
            if(x!=arr.length-1){
                System.out.print(arr[x]+",");
            }else{
                System.out.println(arr[x]+"]");
            }    
        }
    }

 

获取数组最大值,最小值:

    //获取最大值    获取double类型暑促的最大值,定义相同函数名
    public static int getMax(int[] arr){
        int max = arr[0];

        for(int x=1;x<arr.length;x++)
        {
            if(arr[x]>max){
                max = arr[x];
            }
        }
        return max;
    }
    //0作为脚标存在
    public static int getMax_2(int[] arr){
        int max = 0;//如果数组中的数字为负数,则出错

        for(int x=1;x<arr.length;x++)
        {
            if(arr[x]>arr[max]){
                arr[max] = arr[x];
            }
        }
        return arr[max];
    }

    //获取最小值
    public static int getMin(int[] arr){
        int min = arr[0];

        for(int x=1;x<arr.length;x++)
        {
            if(arr[x]<min){
                min = arr[x];
            }
        }
        return min;
    }

排序:

    //位置置换功能
    public static void swap(int[] arr,int x, int y){
        int temp = arr[x];
            arr[x] = arr[y];
            arr[y] = temp;
    }
    //选择排序,升序 x,y代表的都是下标
    public static void selectSort(int[] arr){
        for(int x=0 ; x < arr.length-1; x++)//第一圈
        {
            for(int y = x+1 ; y <arr.length; y++)
            {
                if(arr[x]>arr[y]){
                    swap(arr,x,y);
                    /*int temp = arr[x];
                    arr[x] = arr[y];
                    arr[y] = temp;*/
                }
            }
        }
    }
    
    /*
        冒泡排序,相邻的两个位置进行比较,如果符合条件换位置
        方式一:
    */
    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]){
                    swap(arr,y,y+1);
                    /*int temp = arr[y];
                    arr[y] = arr[y+1];
                    arr[y+1] = temp;*/
                }
            }
        }
    }

    //方式二
    public static void BubbleSort_2(int[] arr){
        for (int x=arr.length-1; x>0 ; x-- )//第一圈
        {
            for (int y=0; y<x;y++)
            {
                if(arr[y]>arr[y+1]){
                    swap(arr,y,y+1);
                    /*int temp = arr[y];
                    arr[y] = arr[y+1];
                    arr[y+1] = temp;*/
                }
            }
        }
    }

    //获取key第一次出现的位置,否则就返回-1
    public static int getIndex(int[] arr,int key)
    {
        for (int x=0; x<arr.length; x++)
        {
            if(arr[x]==key)
                return x;
        }
        return -1;
    }

查找元素Index:

    //获取key第一次出现的位置,否则就返回-1
    public static int getIndex(int[] arr,int key)
    {
        for (int x=0; x<arr.length; x++)
        {
            if(arr[x]==key)
                return x;
        }
        return -1;
    }

查找折半:

//折半查找,提高效率,但需要有序
    public static int halfSearch(int[] arr,int key){
        int min,max,mid;
        min = 0
        max = arr.length -1;
        mid = (min + max)/2;

        while(arr[mid]!=key)
        {
            if(arr[mid]>key)
                min = mid + 1;
            else
                max = mid - 1;
            if(min > max)
                return -1;
            mid = (min+max)/2;
        }
        return mid;
    }

    //折半查找方式二,返回角标
    public static int halfSearch_2(int[] arr,int key){
        int min = 0,max = arr.length-1,mid;
        while(min<=max)
        {
            mid = (min+max)>>1;
            if(key>arr[mid])
                min = mid + 1;
            else
                max = mid -1;
            else
                return mid;
        }
        return -1;
    }

 

 进制转换:

    public static void toBin_4(int num){
        trans(num,1,1);
    }

    public static void toBin_4(int num){
        trans(num,1,1);
    }

    public static void toEight(int num){
        trans(num,7,3);
    }

    public static void toHex(int num){
        trans(num,15,4);
    }
        //抽取的方法
    public static String trans(int num,int base,int offset)
    {
        if(num==0){
            System.out.println(0);
            return;
        }
        char[] chs = {    '0','1','2','3'
                        ,'4','5','6','7'
                        ,'8','9','A','B'
                        ,'C','D','E','F'};
        
        //定义一个临时容器
        char[] arr = new char[32];//'\u0000'
        int pos = arr.length;
        
        //for (int x=0; x<8; x++)
        while(num!=0)
        {
            int temp = num & base;
            arr[--pos] = chs[temp];
            num = num >>> offset;
        }
        
        //遍历数组
        for (int x=pos; x<arr.length; x++)
        {
            System.out.print(arr[x]+",");
        }
    }

    //十进制转成二进制
    public static void toBin(int num){
        //数据容器
        StringBuffer sb = new StringBuffer();
        while(num>0)
        {
            //System.out.println(num%2);
            sb.append(num%2);
            num = num/2;
        }
        System.out.println(sb.reverse());
        
    }

    //查表法
    public static void toBin_2(int num){
        //定义二进制的表
        char[] chs = {'0','1'};

        char[] arr = new char[32];
        
        int pos = arr.length;
        
        while(num!=0)//会循环3次
        {
            int temp = num & 1;
            arr[--pos] = chs[temp];
            num = num >>> 1;//右移两位
        }
        
        //遍历数组
        for (int x=pos; x<arr.length; x++)
        {
            System.out.print(arr[x]+",");
        }
    }

    //十进制转成十六进制
    // 0000-0000 0000-0000 0000-0000 0011-1100
    //&0000-0000 0000-0000 0000-0000 0000-1111  =15
    // 0000-0000 0000-0000 0000-0000 0000-1100  =12  -->C
    public static void toHex(int num)
    {
        //数据容器
        StringBuffer sb = new StringBuffer();
        for(int x =0; x<8; x++)
        {
            int temp = num & 15;
            if(temp>9)
                //System.out.println((char)(n1-10+'A'));
                sb.append((char)(temp-10+'A'));
            else
                //System.out.println(temp);
                sb.append(temp);
            num = num >>> 4;  //右移4位
        }
        System.out.println(sb.reverse());
    }

    /*
        查表法,十进制转为十六进制,数组存值
        0 1 2 3 4 5 6 7 8 9 A  B  C  D  E  F 
        0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
    */
    public static void toHex_2(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;
        }
    }
    
    //使用数组
    public static void toHex_3(int num)
    {
        char[] chs = {    '0','1','2','3'
                        ,'4','5','6','7'
                        ,'8','9','A','B'
                        ,'C','D','E','F'};
        
        //定义一个临时容器
        char[] arr = new char[8];//'\u0000'
        int pos = arr.length;
        
        //for (int x=0; x<8; x++)
        while(num!=0)
        {
            int temp = num & 15;
            arr[--pos] = chs[temp];
            num = num >>> 4;
        }
        
        //遍历数组
        for (int x=pos; x<arr.length; x++)
        {
            System.out.print(arr[x]+",");
        }
    }    

 

 

转载于:https://www.cnblogs.com/colorstory/archive/2013/04/11/2769305.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值